package com.emath.activities;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONException;
import android.animation.Animator;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.os.Vibrator;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ScaleXSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.emath.EnterUsername;
import com.emath.HelpDialog;
import com.lnl.R;
import com.emath.objects.Bitmaparray;
import com.emath.objects.Card;
import com.emath.objects.CardLibrary;
import com.emath.objects.Counter;
import com.emath.objects.CurrentCard;
import com.emath.objects.Game;
import com.emath.objects.OpponentPlayer;
import com.emath.objects.Player;
import com.emath.objects.Tuple;
import com.emath.objects.Username;
import com.emath.shared.BigRational;
import com.emath.shared.ExceptionHandler;
import com.emath.shared.Globals;
import com.emath.shared.RestClient;

public class GamePlay extends Activity {
	public enum Direction {
		VERTICAL, HORIZONTAL
	};
	private static Drawable imageDrawable;
	private EndTurn task3;
	private WaitTurn task;
	private UpdateGame task2;
	private int turn;
	private Handler mHandler = new Handler();
	private int mShortAnimationDuration;
	private Animator mCurrentAnimator;
	boolean flag = false;
	ProgressDialog dialog;
	ProgressDialog dialog2;

	static class count {
		static int count = 0;
	}

	public int rollDie() {

		Random randomGenerator = new Random();
		int x1 = randomGenerator.nextInt(6) + 1;
		int x2 = randomGenerator.nextInt(6) + 1;

		return x1 + x2;
	}
	public static void logHeap() {
		Double allocated = new Double(Debug.getNativeHeapAllocatedSize())
				/ new Double((1048576));
		Double available = new Double(Debug.getNativeHeapSize()) / 1048576.0;
		Double free = new Double(Debug.getNativeHeapFreeSize()) / 1048576.0;
		DecimalFormat df = new DecimalFormat();
		df.setMaximumFractionDigits(2);
		df.setMinimumFractionDigits(2);
		Runtime rt = Runtime.getRuntime();
		long maxMemory = rt.maxMemory();
		Log.v("memoy", "maxMemory:" + Long.toString(maxMemory));
		Log.e("memory", "debug. =================================");
		Log.e("memory", "debug.heap native: allocated " + df.format(allocated)
				+ "MB of " + df.format(available) + "MB (" + df.format(free)
				+ "MB free)");
		Log.e("memory",
				"debug.memory: allocated: "
						+ df.format(new Double(Runtime.getRuntime()
								.totalMemory() / 1048576))
						+ "MB of "
						+ df.format(new Double(
								Runtime.getRuntime().maxMemory() / 1048576))
						+ "MB ("
						+ df.format(new Double(Runtime.getRuntime()
								.freeMemory() / 1048576)) + "MB free)");
	}

	public void decideTurn() {
		/*
		 * Draw 1st card from my deck -> place onto field; Draw 1st card from
		 * opponents deck -> place onto field;
		 */
		// String
		// turn="http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="+Game.GameID+"&turn="+otherplayer;
		// Connect.connect(turn);
		// Connect.clear();
		// new WaitTurn().execute(getApplicationContext());
	}
	protected int sizeOf(Bitmap data) {
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
			return data.getRowBytes() * data.getHeight();
		} else {
			return data.getByteCount();
		}
	}
	public void destroyopponentcards(BigRational amount) {
		double amountleft = amount.doubleValue();

		int id = 0;

		while (amountleft > 0 && Game.opponentnumcharacters() > 0) {
			BigRational min = BigRational.valueOf("100"); // start with large
															// value
			for (int i = 0; i < OpponentPlayer.stack1.size(); i++) {
				switch (i) {
				case 0:
					if (min.compareTo(OpponentPlayer.stack1.get(i).getPower()) > 0
							&& OpponentPlayer.stack1.get(i).cardID != 0
							&& OpponentPlayer.stack1.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack1.get(i).getPower();
						id = R.id.stack1card1;
					}
					break;
				case 1:
					if (min.compareTo(OpponentPlayer.stack1.get(i).getPower()) > 0
							&& OpponentPlayer.stack1.get(i).cardID != 0
							&& OpponentPlayer.stack1.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack1.get(i).getPower();
						id = R.id.stack1card2;
					}
					break;
				case 2:
					if (min.compareTo(OpponentPlayer.stack1.get(i).getPower()) > 0
							&& OpponentPlayer.stack1.get(i).cardID != 0
							&& OpponentPlayer.stack1.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack1.get(i).getPower();
						id = R.id.stack1card3;
					}
					break;
				}
			}
			for (int i = 0; i < OpponentPlayer.stack2.size(); i++) {
				switch (i) {
				case 0:
					if (min.compareTo(OpponentPlayer.stack2.get(i).getPower()) > 0
							&& OpponentPlayer.stack2.get(i).cardID != 0
							&& OpponentPlayer.stack2.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack2.get(i).getPower();
						id = R.id.stack2card1;
					}
					break;
				case 1:
					if (min.compareTo(OpponentPlayer.stack2.get(i).getPower()) > 0
							&& OpponentPlayer.stack2.get(i).cardID != 0
							&& OpponentPlayer.stack2.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack2.get(i).getPower();
						id = R.id.stack2card2;
					}
					break;
				case 2:
					if (min.compareTo(OpponentPlayer.stack2.get(i).getPower()) > 0
							&& OpponentPlayer.stack2.get(i).cardID != 0
							&& OpponentPlayer.stack2.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack2.get(i).getPower();
						id = R.id.stack2card3;
					}
					break;
				}
			}
			for (int i = 0; i < OpponentPlayer.stack3.size(); i++) {
				switch (i) {
				case 0:
					if (min.compareTo(OpponentPlayer.stack3.get(i).getPower()) > 0
							&& OpponentPlayer.stack3.get(i).cardID != 0
							&& OpponentPlayer.stack3.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack3.get(i).getPower();
						id = R.id.stack3card1;
					}
					break;
				case 1:
					if (min.compareTo(OpponentPlayer.stack3.get(i).getPower()) > 0
							&& OpponentPlayer.stack3.get(i).cardID != 0
							&& OpponentPlayer.stack3.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack3.get(i).getPower();
						id = R.id.stack3card2;
					}
					break;
				case 2:
					if (min.compareTo(OpponentPlayer.stack3.get(i).getPower()) > 0
							&& OpponentPlayer.stack3.get(i).cardID != 0
							&& OpponentPlayer.stack3.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack3.get(i).getPower();
						id = R.id.stack3card3;
					}
					break;
				}
			}
			for (int i = 0; i < OpponentPlayer.stack4.size(); i++) {
				switch (i) {
				case 0:
					if (min.compareTo(OpponentPlayer.stack4.get(i).getPower()) > 0
							&& OpponentPlayer.stack4.get(i).cardID != 0
							&& OpponentPlayer.stack4.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack4.get(i).getPower();
						id = R.id.stack4card1;
					}
					break;
				case 1:
					if (min.compareTo(OpponentPlayer.stack4.get(i).getPower()) > 0
							&& OpponentPlayer.stack4.get(i).cardID != 0
							&& OpponentPlayer.stack4.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack4.get(i).getPower();
						id = R.id.stack4card2;
					}
					break;
				case 2:
					if (min.compareTo(OpponentPlayer.stack4.get(i).getPower()) > 0
							&& OpponentPlayer.stack4.get(i).cardID != 0
							&& OpponentPlayer.stack4.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack4.get(i).getPower();
						id = R.id.stack4card3;
					}
					break;
				}
			}
			for (int i = 0; i < OpponentPlayer.stack5.size(); i++) {
				switch (i) {
				case 0:
					if (min.compareTo(OpponentPlayer.stack5.get(i).getPower()) > 0
							&& OpponentPlayer.stack5.get(i).cardID != 0
							&& OpponentPlayer.stack5.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack5.get(i).getPower();
						id = R.id.stack5card1;
					}
					break;
				case 1:
					if (min.compareTo(OpponentPlayer.stack5.get(i).getPower()) > 0
							&& OpponentPlayer.stack5.get(i).cardID != 0
							&& OpponentPlayer.stack5.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack5.get(i).getPower();
						id = R.id.stack5card2;
					}
					break;
				case 2:
					if (min.compareTo(OpponentPlayer.stack5.get(i).getPower()) > 0
							&& OpponentPlayer.stack5.get(i).cardID != 0
							&& OpponentPlayer.stack5.get(i).cardType
									.equals("Character")) {
						min = OpponentPlayer.stack5.get(i).getPower();
						id = R.id.stack5card3;
					}
					break;
				}
			}
			OpponentPlayer.power = OpponentPlayer.power.subtract(min);
			CurrentCard card = new CurrentCard(0);
			ImageView img;
			Drawable drawable;
			String setstacks;
			switch (id) {

			case R.id.stack1card1:
				OpponentPlayer.stack1.set(0, card);
				img = (ImageView) findViewById(R.id.stack1card1);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {

					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack1card2:
				OpponentPlayer.stack1.set(1, card);
				img = (ImageView) findViewById(R.id.stack1card2);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack1card3:
				OpponentPlayer.stack1.set(2, card);
				img = (ImageView) findViewById(R.id.stack1card3);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack2card1:
				OpponentPlayer.stack2.set(0, card);
				img = (ImageView) findViewById(R.id.stack2card1);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack2card2:
				OpponentPlayer.stack2.set(1, card);
				img = (ImageView) findViewById(R.id.stack2card2);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack2card3:
				OpponentPlayer.stack2.set(2, card);
				img = (ImageView) findViewById(R.id.stack2card3);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack3card1:
				OpponentPlayer.stack3.set(0, card);
				img = (ImageView) findViewById(R.id.stack3card1);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack3card2:
				OpponentPlayer.stack3.set(1, card);
				img = (ImageView) findViewById(R.id.stack3card2);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack3card3:
				OpponentPlayer.stack3.set(2, card);
				img = (ImageView) findViewById(R.id.stack3card3);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack4card1:
				OpponentPlayer.stack4.set(0, card);
				img = (ImageView) findViewById(R.id.stack4card1);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack4card2:
				OpponentPlayer.stack4.set(1, card);
				img = (ImageView) findViewById(R.id.stack4card2);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack4card3:
				OpponentPlayer.stack4.set(2, card);
				img = (ImageView) findViewById(R.id.stack4card3);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack5card1:
				OpponentPlayer.stack5.set(0, card);
				img = (ImageView) findViewById(R.id.stack5card1);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack5card2:
				OpponentPlayer.stack5.set(1, card);
				img = (ImageView) findViewById(R.id.stack5card2);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.stack5card3:
				OpponentPlayer.stack5.set(2, card);
				img = (ImageView) findViewById(R.id.stack5card3);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;

			}
			amountleft = amountleft - min.doubleValue();

		}

	}

	public void destroymycards(BigRational amount) {
		double amountleft = amount.doubleValue();

		int id = 0;

		while (amountleft > 0 && Game.numcharacters() > 0) {
			BigRational min = BigRational.valueOf("100"); // start with large
															// value

			for (int i = 0; i < Player.stack1.size(); i++) {
				switch (i) {
				case 0:
					if (min.compareTo(Player.stack1.get(i).getPower()) > 0
							&& Player.stack1.get(i).cardID != 0
							&& Player.stack1.get(i).cardType
									.equals("Character")) {
						min = Player.stack1.get(i).getPower();
						id = R.id.mystack1card1;
					}
					break;
				case 1:
					if (min.compareTo(Player.stack1.get(i).getPower()) > 0
							&& Player.stack1.get(i).cardID != 0
							&& Player.stack1.get(i).cardType
									.equals("Character")) {
						min = Player.stack1.get(i).getPower();
						id = R.id.mystack1card2;
					}
					break;
				case 2:
					if (min.compareTo(Player.stack1.get(i).getPower()) > 0
							&& Player.stack1.get(i).cardID != 0
							&& Player.stack1.get(i).cardType
									.equals("Character")) {
						min = Player.stack1.get(i).getPower();
						id = R.id.mystack1card3;
					}
					break;
				}
			}
			for (int i = 0; i < Player.stack2.size(); i++) {
				switch (i) {
				case 0:
					if (min.compareTo(Player.stack2.get(i).getPower()) > 0
							&& Player.stack2.get(i).cardID != 0
							&& Player.stack2.get(i).cardType
									.equals("Character")) {
						min = Player.stack2.get(i).getPower();
						id = R.id.mystack2card1;
					}
					break;
				case 1:
					if (min.compareTo(Player.stack2.get(i).getPower()) > 0
							&& Player.stack2.get(i).cardID != 0
							&& Player.stack2.get(i).cardType
									.equals("Character")) {
						min = Player.stack2.get(i).getPower();
						id = R.id.mystack2card2;
					}
					break;
				case 2:
					if (min.compareTo(Player.stack2.get(i).getPower()) > 0
							&& Player.stack2.get(i).cardID != 0
							&& Player.stack2.get(i).cardType
									.equals("Character")) {
						min = Player.stack2.get(i).getPower();
						id = R.id.mystack2card3;
					}
					break;
				}
			}
			for (int i = 0; i < Player.stack3.size(); i++) {
				switch (i) {
				case 0:
					if (min.compareTo(Player.stack3.get(i).getPower()) > 0
							&& Player.stack3.get(i).cardID != 0
							&& Player.stack3.get(i).cardType
									.equals("Character")) {
						min = Player.stack3.get(i).getPower();
						id = R.id.mystack3card1;
					}
					break;
				case 1:
					if (min.compareTo(Player.stack3.get(i).getPower()) > 0
							&& Player.stack3.get(i).cardID != 0
							&& Player.stack3.get(i).cardType
									.equals("Character")) {
						min = Player.stack3.get(i).getPower();
						id = R.id.mystack3card2;
					}
					break;
				case 2:
					if (min.compareTo(Player.stack3.get(i).getPower()) > 0
							&& Player.stack3.get(i).cardID != 0
							&& Player.stack3.get(i).cardType
									.equals("Character")) {
						min = Player.stack3.get(i).getPower();
						id = R.id.mystack3card3;
					}
					break;
				}
			}
			for (int i = 0; i < Player.stack4.size(); i++) {
				switch (i) {
				case 0:
					if (min.compareTo(Player.stack4.get(i).getPower()) > 0
							&& Player.stack4.get(i).cardID != 0
							&& Player.stack4.get(i).cardType
									.equals("Character")) {
						min = Player.stack4.get(i).getPower();
						id = R.id.mystack4card1;
					}
					break;
				case 1:
					if (min.compareTo(Player.stack4.get(i).getPower()) > 0
							&& Player.stack4.get(i).cardID != 0
							&& Player.stack4.get(i).cardType
									.equals("Character")) {
						min = Player.stack4.get(i).getPower();
						id = R.id.mystack4card2;
					}
					break;
				case 2:
					if (min.compareTo(Player.stack4.get(i).getPower()) > 0
							&& Player.stack4.get(i).cardID != 0
							&& Player.stack4.get(i).cardType
									.equals("Character")) {
						min = Player.stack4.get(i).getPower();
						id = R.id.mystack4card3;
					}
					break;
				}
			}
			for (int i = 0; i < Player.stack5.size(); i++) {
				switch (i) {
				case 0:
					if (min.compareTo(Player.stack5.get(i).getPower()) > 0
							&& Player.stack5.get(i).cardID != 0
							&& Player.stack5.get(i).cardType
									.equals("Character")) {
						min = Player.stack5.get(i).getPower();
						id = R.id.mystack5card1;
					}
					break;
				case 1:
					if (min.compareTo(Player.stack5.get(i).getPower()) > 0
							&& Player.stack5.get(i).cardID != 0
							&& Player.stack5.get(i).cardType
									.equals("Character")) {
						min = Player.stack5.get(i).getPower();
						id = R.id.mystack5card2;
					}
					break;
				case 2:
					if (min.compareTo(Player.stack5.get(i).getPower()) > 0
							&& Player.stack5.get(i).cardID != 0
							&& Player.stack5.get(i).cardType
									.equals("Character")) {
						min = Player.stack5.get(i).getPower();
						id = R.id.mystack5card3;
					}
					break;
				}
			}
			CurrentCard card = new CurrentCard(0);
			ImageView img;
			Drawable drawable;
			String setstacks;
			switch (id) {

			case R.id.mystack1card1:
				Player.stack1.set(0, card);
				img = (ImageView) findViewById(R.id.mystack1card1);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack1card2:
				Player.stack1.set(1, card);
				img = (ImageView) findViewById(R.id.mystack1card2);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack1card3:
				Player.stack1.set(2, card);
				img = (ImageView) findViewById(R.id.mystack1card3);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack2card1:
				Player.stack2.set(0, card);
				img = (ImageView) findViewById(R.id.mystack2card1);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack2card2:
				Player.stack2.set(1, card);
				img = (ImageView) findViewById(R.id.mystack2card2);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack2card3:
				Player.stack2.set(2, card);
				img = (ImageView) findViewById(R.id.mystack2card3);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack3card1:
				Player.stack3.set(0, card);
				img = (ImageView) findViewById(R.id.mystack3card1);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack3card2:
				Player.stack3.set(1, card);
				img = (ImageView) findViewById(R.id.mystack3card2);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack3card3:
				Player.stack3.set(2, card);
				img = (ImageView) findViewById(R.id.mystack3card3);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack4card1:
				Player.stack4.set(0, card);
				img = (ImageView) findViewById(R.id.mystack4card1);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack4card2:
				Player.stack4.set(1, card);
				img = (ImageView) findViewById(R.id.mystack4card2);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack4card3:
				Player.stack4.set(2, card);
				img = (ImageView) findViewById(R.id.mystack4card3);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack5card1:
				Player.stack5.set(0, card);
				img = (ImageView) findViewById(R.id.mystack5card1);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack5card2:
				Player.stack4.set(1, card);
				img = (ImageView) findViewById(R.id.mystack5card2);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;
			case R.id.mystack5card3:
				Player.stack4.set(2, card);
				img = (ImageView) findViewById(R.id.mystack5card3);
				drawable = img.getDrawable();
				if (drawable instanceof BitmapDrawable) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					if (null != bitmapDrawable
							&& !bitmapDrawable.getBitmap().isRecycled()) {

					} else {

						Log.e("recycled", "bitmap is already recycled");
					}
					bitmapDrawable = null;

				}
				img.clearAnimation();
				img.setVisibility(View.INVISIBLE);

				break;

			}
			amountleft = amountleft - min.doubleValue();

		}

	}

	public void challenge(boolean challenger) throws JSONException {
		final RestClient Connect = new RestClient();
		if (Globals.isconnected== 0){
			Toast.makeText(GamePlay.this, "Connection Error", Toast.LENGTH_LONG).show();
			Globals.isconnected=1;
		}
		else{
		Button challenge = (Button) findViewById(R.id.challenge);

		if (challenger)
			challenge.setVisibility(View.INVISIBLE);
		if (!Player.waschallenged) {
			String getfraction = "http://emath-project.appspot.com/emath/emathservicehttp?function=getaction&gameID="
					+ Game.getGameID()
					+ "&round=challenge"
					+ "&turn="
					+ OpponentPlayer.getPlayer();
			Connect.connect(getfraction);
			int checkconnect = Connect.findindex("action");

			String fraction = Connect.getvalArray().getString(checkconnect);
			List<String> fractions = Arrays.asList(fraction.split("\\s*/\\s*"));

			if (fractions.size() == 1)
				OpponentPlayer.power = BigRational.valueOf(fractions.get(0));
			if (fractions.size() == 2)
				OpponentPlayer.power = BigRational.getInstance(
						fractions.get(0), fractions.get(1));

		} else {
			OpponentPlayer.power = BigRational.valueOf("0");
			Player.waschallenged = false;
		}

		BigRational mypower = Game.getmypowerlevelfraction();
		BigRational opponentpowerwithroll;
		BigRational difference = null;
		String status = null;
		int d1 = 0, d2 = 0;

		// Do luck roll
		if (challenger) {
			d1 = rollDie();
			d2 = rollDie();
			Game.mydiceroll = d2; // set dice roll numbers for opponent to use
			Game.opponentdiceroll = d1;
		}
		if (!challenger) {
			d1 = Game.mydiceroll;
			d2 = Game.opponentdiceroll;
		}
		if (challenger)
			Player.beforechallenge = mypower;

		MediaPlayer mp = MediaPlayer.create(this, R.raw.diceroll);
		mp.start();
		Vibrator vibrator = (Vibrator) getSystemService(Activity.VIBRATOR_SERVICE);
		vibrator.vibrate(Globals.vibrateTime);

		Context context = getApplicationContext(); // toast for dice roll
		CharSequence text = "Your dice rolled ";
		// CharSequence text2 = "Opponent's dice rolled ";
		int duration = Toast.LENGTH_LONG;
		text = text + String.valueOf(d1) + ". ";
		text = text + "Opponent's dice rolled " + String.valueOf(d2) + ".";
		Toast toast = Toast.makeText(context, text, duration);
		toast.setGravity(Gravity.BOTTOM, 230, 400);

		toast.show();

		mypower = mypower.add(BigRational.valueOf(String.valueOf(d1)));
		opponentpowerwithroll = OpponentPlayer.power.add(BigRational
				.valueOf(String.valueOf(d2)));
		if (!challenger)
			OpponentPlayer.power = BigRational.valueOf("0");

		if (opponentpowerwithroll.doubleValue() > mypower.doubleValue()) {
			difference = opponentpowerwithroll.subtract(mypower);
			Player.health = Player.health.subtract(difference);
			status = "lost";
		}

		else if (mypower.doubleValue() > opponentpowerwithroll.doubleValue()) {
			difference = mypower.subtract(opponentpowerwithroll);
			OpponentPlayer.health = OpponentPlayer.health.subtract(difference);
			status = "won";
		} else if (mypower.doubleValue() == opponentpowerwithroll.doubleValue()) {
			difference = BigRational.valueOf("0");
			status = "tie";
		}

		String message = null;
		if (status.equals("won")) {
			message = "You have won the challenge!";
			destroyopponentcards(difference);
			Player.challenged = true;
			String setchallenge = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
					+ Game.getGameID() + "&round=ChallengeWon";
			Connect.connect(setchallenge);
			Connect.clear();
		} else if (status.equals("lost")) {
			message = "You have lost the challenge!";
			destroymycards(difference);
			String setchallenge = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
					+ Game.getGameID() + "&round=ChallengeLost";
			Connect.connect(setchallenge);
			Connect.clear();
		} else if (status.equals("tie")) {
			String setchallenge = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
					+ Game.getGameID() + "&round=ChallengeTie";
			Connect.connect(setchallenge);
			Connect.clear();
			message = "You have tied";
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(GamePlay.this);
		builder.setMessage(message).setNegativeButton("Ok",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {

						TextView theirpower = (TextView) findViewById(R.id.theirpower);
						String theirpowerlv = "Foe Strength: ";
						theirpower.setText(theirpowerlv
								+ Game.toFraction(
										OpponentPlayer.power.doubleValue(),
										1000));

						TextView power = (TextView) findViewById(R.id.mypower);
						String powerlv = "My Strength: ";
						power.setText(powerlv
								+ Game.toFraction(Game.getmypowerlevel(), 1000));

						TextView myhealth = (TextView) findViewById(R.id.theirhealth); // I
																						// know
																						// this
																						// is
																						// reversed.
																						// It
																						// is
																						// a
																						// bug
						String myhealthlv = "My Points: ";
						myhealth.setText(myhealthlv
								+ Game.toFraction(Player.health.doubleValue(),
										1000));

						TextView theirhealth = (TextView) findViewById(R.id.myhealth);
						String healthlv = "Foe Points: ";
						theirhealth.setText(healthlv
								+ Game.toFraction(
										OpponentPlayer.health.doubleValue(),
										1000));
						int endgame;
						if (Player.challenger)
							endgame = endgame(true);
						else
							endgame = endgame(false);
						// Create the AlertDialog object and return it
						if (Player.challenger && endgame != 1) {
							Game.firstturn = false;
							Game.setmystacks();
							Player.waschallenged = false;

							String turn = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
									+ Game.getGameID()
									+ "&turn="
									+ OpponentPlayer.getPlayer()
									+ "&"
									+ Player.getPlayer()
									+ "power="
									+ Game.getmyhealthstring()
									+ "&"
									+ Player.getPlayer()
									+ "strength="
									+ Game.toFractionURL(
											Game.getmypowerlevel(), 1000)
									+ ","
									+ String.valueOf(Game.mydiceroll)
									+ ","
									+ String.valueOf(Game.opponentdiceroll);
							Connect.connect(turn);
							Connect.clear();
							Player.donewaiting = false;
							task = new WaitTurn();
							task.execute(getApplicationContext());
						//	loopupdateboard();
						}

					}

					// User cancelled the dialog

				});
		// Create the AlertDialog object and return it
		builder.create();
		builder.show();
		}
	}

	void quitgame() {
		String message = "Other player has crashed and left the game";
		AlertDialog.Builder builder = new AlertDialog.Builder(GamePlay.this);
		builder.setMessage(message).setNegativeButton("Ok",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {
						RestClient Connect = new RestClient(GamePlay.this);
						String endgame = "http://emath-project.appspot.com/emath/emathservicehttp?function=endgame&gameID="
								+ Game.getGameID();
						Connect.connect(endgame);
						Connect.clear();

						try {
							dialog2.dismiss();
							dialog2 = null;
						} catch (Exception e) {
							// nothing
						}

						Connect.clear();
						Game.exit = true;
						task2.cancel(true);
						finish();

						// User cancelled the dialog
					}
				});
		builder.create();
		builder.show();

	}

	int endgame(boolean challenger) {
		final RestClient Connect = new RestClient();

		if (challenger) {
			if (Game.endgame() == 1) {
				String message = "Game Over. You have lost the game";
				AlertDialog.Builder builder = new AlertDialog.Builder(
						GamePlay.this);
				builder.setMessage(message).setNegativeButton("Ok",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								Game.setmystacks();

								Game.firstturn = false;
								Game.setmystacks();
								Player.waschallenged = false;
								String turn = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
										+ Game.getGameID()
										+ "&turn="
										+ OpponentPlayer.getPlayer()
										+ "&"
										+ Player.getPlayer()
										+ "power="
										+ Game.getmyhealthstring()
										+ "&"
										+ Player.getPlayer()
										+ "strength="
										+ Game.toFractionURL(
												Game.getmypowerlevel(), 1000)
										+ ","
										+ String.valueOf(Game.mydiceroll)
										+ ","
										+ String.valueOf(Game.opponentdiceroll);

								Connect.connect(turn);
								Connect.clear();
								if (task2 != null && !task2.isCancelled())
									task2.cancel(true);

								finish();

								// User cancelled the dialog
							}
						});
				// Create the AlertDialog object and return it
				builder.create();
				builder.show();
				return 1;
			}
			if (Game.endgame() == 2) {
				String message = "You have won the game!";
				AlertDialog.Builder builder = new AlertDialog.Builder(
						GamePlay.this);
				builder.setMessage(message).setNegativeButton("Ok",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								Game.setmystacks();

								Game.firstturn = false;
								Game.setmystacks();
								Player.waschallenged = false;
								String turn = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
										+ Game.getGameID()
										+ "&turn="
										+ OpponentPlayer.getPlayer()
										+ "&"
										+ Player.getPlayer()
										+ "power="
										+ Game.getmyhealthstring()
										+ "&"
										+ Player.getPlayer()
										+ "strength="
										+ Game.toFractionURL(
												Game.getmypowerlevel(), 1000)
										+ ","
										+ String.valueOf(Game.mydiceroll)
										+ ","
										+ String.valueOf(Game.opponentdiceroll);
								Connect.connect(turn);
								Connect.clear();

								String gettoken = "http://emath-project.appspot.com/emath/emathservicehttp?function=getuser&userID="
										+ Username.getUser();
								Connect.connect(gettoken);
								int checkconnect = Connect.findindex("tokens");
								String settoken = null;
								try {
									int token = Integer
											.parseInt(Connect.valArray
													.getString(checkconnect));
									token = token + 1;
									checkconnect = Connect
											.findindex("skillLevel");
									int skillevel = Integer
											.parseInt(Connect.valArray
													.getString(checkconnect));
									skillevel = skillevel + 1;
									settoken = "http://emath-project.appspot.com/emath/emathservicehttp?function=setuser&userID="
											+ Username.getUser()
											+ "&tokens="
											+ String.valueOf(token)
											+ "&skillLevel="
											+ String.valueOf(skillevel);
									Connect.connect(settoken);
								} catch (JSONException e1) {
									e1.printStackTrace();
								}
								if (task2 != null && !task2.isCancelled())
									task2.cancel(true);

								finish();

								// User cancelled the dialog
							}
						});
				// Create the AlertDialog object and return it
				builder.create();
				builder.show();
				return 1;
			}
		}
		if (!challenger) {
			if (Game.endgame() == 1) {
				String message = "Game Over. You have lost the game";
				AlertDialog.Builder builder = new AlertDialog.Builder(
						GamePlay.this);
				builder.setMessage(message).setNegativeButton("Ok",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								((AlertDialog) dialog).getButton(id)
										.setVisibility(View.INVISIBLE);

								String finished = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
										+ Game.getGameID()
										+ "&"
										+ "round="
										+ "finished";
								Connect.connect(finished);
								String endgame = "http://emath-project.appspot.com/emath/emathservicehttp?function=endgame&gameID="
										+ Game.getGameID();
								Connect.connect(endgame);
								if (task2 != null && !task2.isCancelled())
									task2.cancel(true);

								finish();

								// User cancelled the dialog
								((AlertDialog) dialog).getButton(id)
										.setVisibility(View.VISIBLE);
							}
						});
				// Create the AlertDialog object and return it
				builder.create();
				builder.show();
				return 1;
			}
			if (Game.endgame() == 2) {
				String message = "You have won the game!";
				AlertDialog.Builder builder = new AlertDialog.Builder(
						GamePlay.this);
				builder.setMessage(message).setNegativeButton("Ok",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								((AlertDialog) dialog).getButton(id)
										.setVisibility(View.INVISIBLE);
								String finished = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
										+ Game.getGameID()
										+ "&"
										+ "round="
										+ "finished";
								Connect.connect(finished);
								String endgame = "http://emath-project.appspot.com/emath/emathservicehttp?function=endgame&gameID="
										+ Game.getGameID();
								Connect.connect(endgame);

								String gettoken = "http://emath-project.appspot.com/emath/emathservicehttp?function=getuser&userID="
										+ Username.getUser();
								Connect.connect(gettoken);
								int checkconnect = Connect.findindex("tokens");
								String settoken = null;
								try {
									int token = Integer
											.parseInt(Connect.valArray
													.getString(checkconnect));
									token = token + 1;
									checkconnect = Connect
											.findindex("skillLevel");
									int skillevel = Integer
											.parseInt(Connect.valArray
													.getString(checkconnect));
									skillevel = skillevel + 1;
									settoken = "http://emath-project.appspot.com/emath/emathservicehttp?function=setuser&userID="
											+ Username.getUser()
											+ "&tokens="
											+ String.valueOf(token)
											+ "&skillLevel="
											+ String.valueOf(skillevel);
									Connect.connect(settoken);
								} catch (JSONException e1) {
									e1.printStackTrace();
								}
								if (task2 != null && !task2.isCancelled())
									task2.cancel(true);

								finish();
								((AlertDialog) dialog).getButton(id)
										.setVisibility(View.VISIBLE);
								// User cancelled the dialog
							}
						});
				// Create the AlertDialog object and return it
				builder.create();
				builder.show();
				return 1;
			}
		}

		return 0;

	}

	public BitmapDrawable writeOnDrawable(int drawableId, CurrentCard card,
			int cardID) throws IOException {

		int drawableid = 0;
		int size;
		final float scale = getResources().getDisplayMetrics().density;

		if (card.getCardType().equals("Resource"))
			drawableid = getResources().getIdentifier("sample_2", "drawable",
					getPackageName());
		if (card.getType1().equals("Student"))
			drawableid = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
		if (card.getType1().equals("Leader"))
			drawableid = getResources().getIdentifier("sample_1", "drawable",
					getPackageName());
		if (card.getType1().equals("Artist"))
			drawableid = getResources().getIdentifier("sample_3", "drawable",
					getPackageName());
		if (card.getType1().equals("Athlete"))
			drawableid = getResources().getIdentifier("sample_4", "drawable",
					getPackageName());
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inPurgeable = true;
		MemoryInfo mi = new MemoryInfo();
		ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		activityManager.getMemoryInfo(mi);
		long availableMegs = mi.availMem / 1048576L;
		Log.e("memory", String.valueOf(availableMegs));
		Bitmap original = BitmapFactory.decodeResource(getResources(),
				drawableid, options);
		int si = sizeOf(original);
		Log.e("memory", String.valueOf(si));
		//logHeap();
		Bitmap bm = original.copy(Bitmap.Config.RGB_565, true);
		int si2 = sizeOf(bm);
		Log.e("memory", String.valueOf(si2));
		original.recycle();

		Paint paint = new Paint();
		paint.setStyle(Style.FILL);
		paint.setColor(Color.BLACK);

		size = (int) (33.33 * scale + 0.5f);
		paint.setTextSize(size);

		Paint paint2 = new Paint();// for drawing the levelups
		paint2.setStyle(Style.FILL);
		paint2.setColor(Color.BLACK);
		size = (int) (16.66 * scale + 0.5f);
		paint2.setTextSize(size);

		Canvas canvas = new Canvas(bm);
		String mind = "M " + String.valueOf(card.getMind());
		String strength = "S " + String.valueOf(card.getStrength());
		String charm = "C " + String.valueOf(card.getCharm());
		String type = "";
		String name = card.name;

		if (!card.type1.equals(""))
			type = type + card.type1 + " ";
		if (!card.type2.equals(""))
			type = type + card.type2 + " ";
		if (!card.type3.equals(""))
			type = type + card.type3 + " ";

		String cost = card.cost;
		String levelups = "";
		for (int i = 0; i < 3; i++) {
			switch (i) {
			case 0:
				if (!card.getLevelUp1().equals(""))
					levelups = levelups + card.getLevelUp1() + " ";
				break;
			case 1:
				if (!card.getLevelUp2().equals(""))
					levelups = levelups + card.getLevelUp2() + " ";
				break;
			case 3:
				if (!card.getLevelUp3().equals(""))
					levelups = levelups + card.getLevelUp3() + " ";
				break;
			}

		}

		Paint paint3 = new Paint(); // for drawing the levelups
		paint3.setStyle(Style.FILL);
		paint3.setColor(Color.BLACK);
		size = (int) (10 * scale + 0.5f);
		paint3.setTextSize(size);

		canvas.drawText(cost, (float) (.057 * bm.getWidth()),
				(bm.getHeight() / 2), paint);
		canvas.drawText(type, (float) (.057 * bm.getWidth()),
				(float) (.933 * bm.getHeight()), paint3);

		canvas.drawText(mind, (float) (.057 * bm.getWidth()),
				(float) ((bm.getHeight() / 2) - (.363 * bm.getHeight())), paint);
		canvas.drawText(charm, (float) (.4022 * bm.getWidth()),
				(float) ((bm.getHeight() / 2) - (.363 * bm.getHeight())), paint);
		canvas.drawText(strength, (float) (.7327 * bm.getWidth()),
				(float) ((bm.getHeight() / 2) - (.363 * bm.getHeight())), paint);
		canvas.rotate(90, bm.getWidth() / 2, bm.getHeight() / 2);
		canvas.drawText(levelups, (float) (.0718 * bm.getWidth()) + 5,
				(float) ((bm.getHeight() / 2) - (.339 * bm.getHeight())) + 15,
				paint3);
		canvas.rotate(270, bm.getWidth() / 2, bm.getHeight() / 2);
		float x = (float) (.905 * bm.getWidth());
		size = (int) (13.33 * scale + 0.5f);
		paint3.setTextSize(size);
		size = (int) (6.66 * scale + 0.5f);

		if (!card.cardType.equals("Resource")) {
			for (int i = 0; i < card.getLevel(); i++) {
				canvas.drawCircle(x, (float) (.932 * (bm.getHeight())), size,
						paint3);
				x = (float) (x - (.07184 * bm.getWidth()));
			}
		}
		int width = (int) (120 * scale + 0.5f);
		int height = (int) (133.33 * scale + 0.5f);
		DisplayMetrics metrics = getResources().getDisplayMetrics();

		Bitmap image = Bitmap.createScaledBitmap((Globals.libraryObj
				.getCardbyID(String.valueOf(card.getCardID()))).getBitmap(),
				width, height, false);

		canvas.drawBitmap(image, (float) (.259 * bm.getWidth()),
				(float) (.157 * bm.getHeight()), paint2);
		image.recycle();
		image = null;
		canvas.drawText(name, (float) (.0575 * bm.getWidth()),
				(float) (.884 * bm.getHeight()), paint2);

		return new BitmapDrawable(getApplicationContext().getResources(), bm);
	}

	public BitmapDrawable writeOnDrawableReverse(int drawableId,
			CurrentCard currentCard, int cardID) {
		int size;
		int drawableid = 0;
		final float scale = getResources().getDisplayMetrics().density;

		if (currentCard.getCardType().equals("Resource"))
			drawableid = getResources().getIdentifier("sample_2reverse",
					"drawable", getPackageName());
		if (currentCard.getType1().equals("Student"))
			drawableid = getResources().getIdentifier("sample_0reverse",
					"drawable", getPackageName());
		if (currentCard.getType1().equals("Leader"))
			drawableid = getResources().getIdentifier("sample_1reverse",
					"drawable", getPackageName());
		if (currentCard.getType1().equals("Artist"))
			drawableid = getResources().getIdentifier("sample_3reverse",
					"drawable", getPackageName());
		if (currentCard.getType1().equals("Athlete"))
			drawableid = getResources().getIdentifier("sample_4reverse",
					"drawable", getPackageName());
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inPurgeable = true;
		Bitmap original = BitmapFactory.decodeResource(getResources(),
				drawableid, options);
		Bitmap bm = original.copy(Bitmap.Config.RGB_565, true);
		original.recycle();

		Paint paint = new Paint();
		paint.setStyle(Style.FILL);
		paint.setColor(Color.BLACK);
		size = (int) (33.33 * scale + 0.5f);
		paint.setTextSize(size);

		Paint paint2 = new Paint();
		paint2.setStyle(Style.FILL);
		paint2.setColor(Color.BLACK);
		size = (int) (16.66 * scale + 0.5f);
		paint2.setTextSize(size);

		Canvas canvas = new Canvas(bm);

		canvas.rotate(180, bm.getWidth() / 2, bm.getHeight() / 2);
		String mind = "M " + currentCard.getMind();
		String strength = "S " + currentCard.getStrength();
		String charm = "C " + currentCard.getCharm();
		String type = "";
		String cost = currentCard.cost;
		String name = currentCard.getName();
		if (!currentCard.type1.equals(""))
			type = type + currentCard.type1 + " ";
		if (!currentCard.type2.equals(""))
			type = type + currentCard.type2 + " ";
		if (!currentCard.type3.equals(""))
			type = type + currentCard.type3 + " ";

		Paint paint3 = new Paint(); // for drawing the levelups
		paint3.setStyle(Style.FILL);
		paint3.setColor(Color.BLACK);
		size = (int) (10 * scale + 0.5f);
		paint3.setTextSize(size);

		canvas.drawText(cost, (float) (.057 * bm.getWidth()),
				(bm.getHeight() / 2), paint);
		canvas.drawText(type, (float) (.057 * bm.getWidth()),
				(float) (.933 * bm.getHeight()), paint3);

		canvas.drawText(mind, (float) (.057 * bm.getWidth()),
				(float) ((bm.getHeight() / 2) - (.363 * bm.getHeight())), paint);
		canvas.drawText(charm, (float) (.4022 * bm.getWidth()),
				(float) ((bm.getHeight() / 2) - (.363 * bm.getHeight())), paint);
		canvas.drawText(strength, (float) (.7327 * bm.getWidth()),
				(float) ((bm.getHeight() / 2) - (.363 * bm.getHeight())), paint);
		canvas.rotate(90, bm.getWidth() / 2, bm.getHeight() / 2);
		canvas.rotate(270, bm.getWidth() / 2, bm.getHeight() / 2);
		float x = (float) (.905 * bm.getWidth());
		size = (int) (13.33 * scale + 0.5f);
		paint3.setTextSize(size);
		size = (int) (6.66 * scale + 0.5f);

		if (!currentCard.cardType.equals("Resource")) {
			for (int i = 0; i < currentCard.getLevel(); i++) {
				canvas.drawCircle(x, (float) (.932 * (bm.getHeight())), size,
						paint3);
				x = (float) (x - (.07184 * bm.getWidth()));
			}
		}
		int width = (int) (120 * scale + 0.5f);
		int height = (int) (133.33 * scale + 0.5f);
		DisplayMetrics metrics = getResources().getDisplayMetrics();

		Bitmap image = Bitmap.createScaledBitmap((Globals.libraryObj
				.getCardbyID(String.valueOf(currentCard.getCardID())))
				.getBitmap(), width, height, false);

		canvas.drawBitmap(image, (float) (.259 * bm.getWidth()),
				(float) (.157 * bm.getHeight()), paint2);
		image.recycle();
		image = null;
		canvas.drawText(name, (float) (.0575 * bm.getWidth()),
				(float) (.884 * bm.getHeight()), paint2);

		return new BitmapDrawable(getApplicationContext().getResources(), bm);
	}

	void gethand() { // implement

	}

	void turnallcards() {

		for (int i = 0; i < Player.stack1.size(); i++)
			if (Player.stack1.get(i).cardType.equals("Character")) {
				Player.stack1.get(i).tapped = true;
				ImageView img = null;
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.mystack1card1);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.mystack1card2);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.mystack1card3);
					break;
				}

				RotateAnimation anim2 = new RotateAnimation(0, 90,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				anim2.setFillAfter(true);
				anim2.setDuration(100);
				img.startAnimation(anim2);
			}

		for (int i = 0; i < Player.stack2.size(); i++)
			if (Player.stack2.get(i).cardType.equals("Character")) {
				Player.stack2.get(i).tapped = true;
				ImageView img = null;
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.mystack2card1);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.mystack2card2);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.mystack2card3);
					break;
				}

				RotateAnimation anim2 = new RotateAnimation(0, 90,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				anim2.setFillAfter(true);
				anim2.setDuration(100);
				img.startAnimation(anim2);
			}

		for (int i = 0; i < Player.stack3.size(); i++)
			if (Player.stack3.get(i).cardType.equals("Character")) {
				Player.stack3.get(i).tapped = true;
				ImageView img = null;
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.mystack3card1);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.mystack3card2);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.mystack3card3);
					break;
				}

				RotateAnimation anim2 = new RotateAnimation(0, 90,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				anim2.setFillAfter(true);
				anim2.setDuration(100);
				img.startAnimation(anim2);
			}
		for (int i = 0; i < Player.stack4.size(); i++)
			if (Player.stack4.get(i).cardType.equals("Character")) {
				Player.stack4.get(i).tapped = true;
				ImageView img = null;
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.mystack4card1);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.mystack4card2);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.mystack4card3);
					break;
				}

				RotateAnimation anim2 = new RotateAnimation(0, 90,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				anim2.setFillAfter(true);
				anim2.setDuration(100);
				img.startAnimation(anim2);
			}

		for (int i = 0; i < Player.stack5.size(); i++)
			if (Player.stack5.get(i).cardType.equals("Character")) {
				Player.stack5.get(i).tapped = true;
				ImageView img = null;
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.mystack5card1);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.mystack5card2);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.mystack5card3);
					break;
				}

				RotateAnimation anim2 = new RotateAnimation(0, 90,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				anim2.setFillAfter(true);
				anim2.setDuration(100);
				img.startAnimation(anim2);
			}
		TextView power = (TextView) findViewById(R.id.mypower);
		String powerlv = "My Strength: ";
		power.setText(powerlv + Game.toFraction(Game.getmypowerlevel(), 1000));

	}

	void unturncards() {

		for (int i = 0; i < Player.stack1.size(); i++)
			if (Player.stack1.get(i).tapped) {

				Player.stack1.get(i).tapped = false;
				ImageView img = null;
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.mystack1card1);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.mystack1card2);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.mystack1card3);
					break;
				}
				RotateAnimation anim2 = new RotateAnimation(0, 360,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				anim2.setFillAfter(true);
				anim2.setDuration(100);
				img.startAnimation(anim2);

			}
		for (int i = 0; i < Player.stack2.size(); i++)
			if (Player.stack2.get(i).tapped) {

				Player.stack2.get(i).tapped = false;
				ImageView img = null;
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.mystack2card1);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.mystack2card2);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.mystack2card3);
					break;
				}

				RotateAnimation anim2 = new RotateAnimation(0, 360,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				anim2.setFillAfter(true);
				anim2.setDuration(100);
				img.startAnimation(anim2);
			}
		for (int i = 0; i < Player.stack3.size(); i++)
			if (Player.stack3.get(i).tapped) {

				Player.stack3.get(i).tapped = false;
				ImageView img = null;
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.mystack3card1);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.mystack3card2);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.mystack3card3);
					break;
				}

				RotateAnimation anim2 = new RotateAnimation(0, 360,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				anim2.setFillAfter(true);
				anim2.setDuration(100);
				img.startAnimation(anim2);
			}
		for (int i = 0; i < Player.stack4.size(); i++)
			if (Player.stack4.get(i).tapped) {

				Player.stack4.get(i).tapped = false;
				ImageView img = null;
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.mystack4card1);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.mystack4card2);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.mystack4card3);
					break;
				}

				RotateAnimation anim2 = new RotateAnimation(0, 360,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				anim2.setFillAfter(true);
				anim2.setDuration(100);
				img.startAnimation(anim2);
			}
		for (int i = 0; i < Player.stack5.size(); i++)
			if (Player.stack5.get(i).tapped) {

				Player.stack5.get(i).tapped = false;
				ImageView img = null;
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.mystack5card1);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.mystack5card2);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.mystack5card3);
					break;
				}

				RotateAnimation anim2 = new RotateAnimation(0, 360,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				anim2.setFillAfter(true);
				anim2.setDuration(100);
				img.startAnimation(anim2);
			}

	}

	int turncards(CurrentCard card) {

		int count = 0;
		int limit = Integer.parseInt(card.cost);
		for (int i = 0; i < Player.stack1.size(); i++)
			if (!Player.stack1.get(i).tapped
					&& Player.stack1.get(i).cardType.equals("Resource")) {
				if (count + 1 > limit)
					return 0;
				count++;
				Player.stack1.get(i).tapped = true;
				ImageView img = null;
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.mystack1card1);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.mystack1card2);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.mystack1card3);
					break;
				}

				RotateAnimation anim2 = new RotateAnimation(0, 90,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				anim2.setFillAfter(true);
				anim2.setDuration(100);
				img.startAnimation(anim2);
			}

		return 0;

	}

	boolean candraw() {
		for (int i = 0; i < 6; i++)
			if (Player.mydeck.get(i).cardID != 0)
				return true;
		return false;
	}

	int outofcards() {
		final RestClient Connect = new RestClient(GamePlay.this);
		if (Player.mydeck.size() == 0) {

			if (Game.getmypowerlevel() > OpponentPlayer.power.doubleValue()) {
				String message = "Out of cards in Deck. You have won the game with greater power!";
				AlertDialog.Builder builder = new AlertDialog.Builder(
						GamePlay.this);
				builder.setMessage(message).setNegativeButton("Ok",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								((AlertDialog) dialog).getButton(id)
										.setVisibility(View.INVISIBLE);
								Game.setmystacks();

								Game.firstturn = false;
								Game.setmystacks();
								Player.waschallenged = false;
								String turn = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
										+ Game.getGameID()
										+ "&turn="
										+ OpponentPlayer.getPlayer()
										+ "&"
										+ Player.getPlayer()
										+ "power="
										+ Game.getmyhealthstring()
										+ "&"
										+ Player.getPlayer()
										+ "strength="
										+ Game.toFractionURL(
												Game.getmypowerlevel(), 1000)
										+ ","
										+ String.valueOf(Game.mydiceroll)
										+ ","
										+ String.valueOf(Game.opponentdiceroll)
										+ "&round=Outofcardslost";
								Connect.connect(turn);
								Connect.clear();

								String gettoken = "http://emath-project.appspot.com/emath/emathservicehttp?function=getuser&userID="
										+ Username.getUser();
								Connect.connect(gettoken);
								int checkconnect = Connect.findindex("tokens");
								String settoken = null;
								try {
									int token = Integer
											.parseInt(Connect.valArray
													.getString(checkconnect));
									token = token + 1;
									checkconnect = Connect
											.findindex("skillLevel");
									int skillevel = Integer
											.parseInt(Connect.valArray
													.getString(checkconnect));
									skillevel = skillevel + 1;
									settoken = "http://emath-project.appspot.com/emath/emathservicehttp?function=setuser&userID="
											+ Username.getUser()
											+ "&tokens="
											+ String.valueOf(token)
											+ "&skillLevel="
											+ String.valueOf(skillevel);
									Connect.connect(settoken);
								} catch (JSONException e1) {
									e1.printStackTrace();
								}
								if (task2 != null && !task2.isCancelled())
									task2.cancel(true);

								finish();
								((AlertDialog) dialog).getButton(id)
										.setVisibility(View.VISIBLE);
								// User cancelled the dialog
							}
						});
				// Create the AlertDialog object and return it
				builder.create();
				builder.show();

			}
			if (Game.getmypowerlevel() < OpponentPlayer.power.doubleValue()) {
				String message = "Game Over. Out of cards in deck. You have lost the game with weaker power";
				AlertDialog.Builder builder = new AlertDialog.Builder(
						GamePlay.this);
				builder.setMessage(message).setNegativeButton("Ok",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								((AlertDialog) dialog).getButton(id)
										.setVisibility(View.INVISIBLE);
								Game.setmystacks();

								Game.firstturn = false;
								Game.setmystacks();
								Player.waschallenged = false;
								String turn = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
										+ Game.getGameID()
										+ "&turn="
										+ OpponentPlayer.getPlayer()
										+ "&"
										+ Player.getPlayer()
										+ "power="
										+ Game.getmyhealthstring()
										+ "&"
										+ Player.getPlayer()
										+ "strength="
										+ Game.toFractionURL(
												Game.getmypowerlevel(), 1000)
										+ ","
										+ String.valueOf(Game.mydiceroll)
										+ ","
										+ String.valueOf(Game.opponentdiceroll)
										+ "&round=Outofcardswon";
								Connect.connect(turn);
								Connect.clear();
								if (task2 != null && !task2.isCancelled())
									task2.cancel(true);

								finish();
								((AlertDialog) dialog).getButton(id)
										.setVisibility(View.VISIBLE);
								// User cancelled the dialog
							}
						});
				// Create the AlertDialog object and return it
				builder.create();
				builder.show();

			}
			if (Game.getmypowerlevel() == OpponentPlayer.power.doubleValue()) {
				String message = "Game Over. Out of cards in deck. You have tied on equal power";
				AlertDialog.Builder builder = new AlertDialog.Builder(
						GamePlay.this);
				builder.setMessage(message).setNegativeButton("Ok",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								((AlertDialog) dialog).getButton(id)
										.setVisibility(View.INVISIBLE);
								Game.setmystacks();

								Game.firstturn = false;
								Game.setmystacks();
								Player.waschallenged = false;
								String turn = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
										+ Game.getGameID()
										+ "&turn="
										+ OpponentPlayer.getPlayer()
										+ "&"
										+ Player.getPlayer()
										+ "power="
										+ Game.getmyhealthstring()
										+ "&"
										+ Player.getPlayer()
										+ "strength="
										+ Game.toFractionURL(
												Game.getmypowerlevel(), 1000)
										+ ","
										+ String.valueOf(Game.mydiceroll)
										+ ","
										+ String.valueOf(Game.opponentdiceroll);
								Connect.connect(turn);
								Connect.clear();
								if (task2 != null && !task2.isCancelled())
									task2.cancel(true);

								finish();
								((AlertDialog) dialog).getButton(id)
										.setVisibility(View.VISIBLE);
								// User cancelled the dialog
							}
						});
				// Create the AlertDialog object and return it
				builder.create();
				builder.show();

			}
			return 1;
		}
		return 0;
	}

	void drawcard() throws IOException {
		ImageView img;
		int id;
		Drawable drawable;
		for (int i = 0; i < 6; i++)
			if (Player.myhand.get(i).cardID == 0) {
				Player.myhand.set(i, Player.mydeck.get(0));
				Player.mydeck.remove(0);
				switch (i) {
				case 0:
					img = (ImageView) findViewById(R.id.myhandcard1);
					id = getResources().getIdentifier("sample_0", "drawable",
							getPackageName());
					drawable = img.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawable(id,
							Player.myhand.get(0), Player.myhand.get(0).cardID));
					img.setVisibility(View.VISIBLE);
					break;
				case 1:
					img = (ImageView) findViewById(R.id.myhandcard2);
					id = getResources().getIdentifier("sample_0", "drawable",
							getPackageName());
					drawable = img.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawable(id,
							Player.myhand.get(1), Player.myhand.get(1).cardID));
					img.setVisibility(View.VISIBLE);
					break;
				case 2:
					img = (ImageView) findViewById(R.id.myhandcard3);
					id = getResources().getIdentifier("sample_0", "drawable",
							getPackageName());
					drawable = img.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawable(id,
							Player.myhand.get(2), Player.myhand.get(2).cardID));
					img.setVisibility(View.VISIBLE);
					break;
				case 3:
					img = (ImageView) findViewById(R.id.myhandcard4);
					id = getResources().getIdentifier("sample_0", "drawable",
							getPackageName());
					drawable = img.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {
							// bitmapDrawable.getBitmap().recycle();
						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawable(id,
							Player.myhand.get(3), Player.myhand.get(3).cardID));
					img.setVisibility(View.VISIBLE);
					break;
				case 4:
					img = (ImageView) findViewById(R.id.myhandcard5);
					id = getResources().getIdentifier("sample_0", "drawable",
							getPackageName());
					drawable = img.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawable(id,
							Player.myhand.get(4), Player.myhand.get(4).cardID));
					img.setVisibility(View.VISIBLE);
					break;
				case 5:
					img = (ImageView) findViewById(R.id.myhandcard6);
					id = getResources().getIdentifier("sample_0", "drawable",
							getPackageName());
					drawable = img.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawable(id,
							Player.myhand.get(5), Player.myhand.get(5).cardID));
					img.setVisibility(View.VISIBLE);
					break;

				}
				break;
			}

	}

	void discardanduntap() {
		RotateAnimation anim2;
		ImageView img;
		Drawable drawable;

		if (OpponentPlayer.stack1.get(0).cardID == 0) { // remove discarded
														// cards
			img = (ImageView) findViewById(R.id.stack1card1);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack1.get(1).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack1card2);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack1.get(2).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack1card3);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack2.get(0).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack2card1);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack2.get(1).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack2card2);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack2.get(2).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack2card3);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack3.get(0).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack3card1);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack3.get(1).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack3card2);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack3.get(2).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack3card3);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack4.get(0).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack4card1);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack4.get(1).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack4card2);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack4.get(2).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack4card3);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack5.get(0).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack5card1);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack5.get(1).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack5card2);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		if (OpponentPlayer.stack5.get(2).cardID == 0) {
			img = (ImageView) findViewById(R.id.stack5card3);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);
		}

		// untap tapped cards
		if (OpponentPlayer.stack1.get(0).cardID != 0
				&& !OpponentPlayer.stack1.get(0).tapped) {
			img = (ImageView) findViewById(R.id.stack1card1);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack1.get(1).cardID != 0
				&& !OpponentPlayer.stack1.get(1).tapped) {
			img = (ImageView) findViewById(R.id.stack1card2);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack1.get(2).cardID != 0
				&& !OpponentPlayer.stack1.get(2).tapped) {
			img = (ImageView) findViewById(R.id.stack1card3);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack2.get(0).cardID != 0
				&& !OpponentPlayer.stack2.get(0).tapped) {
			img = (ImageView) findViewById(R.id.stack2card1);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack2.get(1).cardID != 0
				&& !OpponentPlayer.stack2.get(1).tapped) {
			img = (ImageView) findViewById(R.id.stack2card2);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack2.get(2).cardID != 0
				&& !OpponentPlayer.stack2.get(2).tapped) {
			img = (ImageView) findViewById(R.id.stack2card3);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack3.get(0).cardID != 0
				&& !OpponentPlayer.stack3.get(0).tapped) {
			img = (ImageView) findViewById(R.id.stack3card1);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack3.get(1).cardID != 0
				&& !OpponentPlayer.stack3.get(1).tapped) {
			img = (ImageView) findViewById(R.id.stack3card2);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack3.get(2).cardID != 0
				&& !OpponentPlayer.stack3.get(2).tapped) {
			img = (ImageView) findViewById(R.id.stack3card3);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack4.get(0).cardID != 0
				&& !OpponentPlayer.stack4.get(0).tapped) {
			img = (ImageView) findViewById(R.id.stack4card1);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack4.get(1).cardID != 0
				&& !OpponentPlayer.stack4.get(1).tapped) {
			img = (ImageView) findViewById(R.id.stack4card2);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack4.get(2).cardID != 0
				&& !OpponentPlayer.stack4.get(2).tapped) {
			img = (ImageView) findViewById(R.id.stack4card3);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack5.get(0).cardID != 0
				&& !OpponentPlayer.stack5.get(0).tapped) {
			img = (ImageView) findViewById(R.id.stack5card1);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack5.get(1).cardID != 0
				&& !OpponentPlayer.stack5.get(1).tapped) {
			img = (ImageView) findViewById(R.id.stack5card2);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

		if (OpponentPlayer.stack5.get(2).cardID != 0
				&& !OpponentPlayer.stack5.get(2).tapped) {
			img = (ImageView) findViewById(R.id.stack5card3);
			img.clearAnimation();
			img.setVisibility(View.INVISIBLE);

		}

	}
	void clearopponentstacks() {
		RotateAnimation anim2;
		ImageView img;
		Drawable drawable;
		img = (ImageView) findViewById(R.id.stack1card1);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack1card2);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack1card3);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack2card1);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack2card2);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack2card3);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack3card1);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack3card2);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack3card3);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack4card1);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack4card2);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack4card3);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack5card1);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack5card2);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.stack5card3);
		img.clearAnimation();
		img.setVisibility(View.INVISIBLE);

	}

	void updatestrength() {
		TextView power = (TextView) findViewById(R.id.mypower);
		String powerlv = "My Strength: ";
		power.setText(powerlv + Game.toFraction(Game.getmypowerlevel(), 1000));

	}
	void loopupdateboard() {
		final Handler mHandler = new Handler();
		final int wait = 0;
		mHandler.removeCallbacks(updateboardloop);
		mHandler.postDelayed(updateboardloop, 100);
	}

	void updateboard() throws JSONException {
		RestClient Connect = new RestClient(GamePlay.this);

		String p = OpponentPlayer.getPlayer();
		List<String> strengthanddiceroll;

		String strength = "";
		String getstrength = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + p + "strength";
		Connect.connect(getstrength);
		int checkconnect = Connect.findindex(p + "strength");
		if (checkconnect != -1) {
			strengthanddiceroll = Game.parsecards(Connect.valArray
					.getString(checkconnect));
			strength = strengthanddiceroll.get(0);
			Game.mydiceroll = Integer.parseInt(strengthanddiceroll.get(1));
			Game.opponentdiceroll = Integer
					.parseInt(strengthanddiceroll.get(2));

			TextView power = (TextView) findViewById(R.id.theirpower);
			String powerlv = "Foe Strength: ";
			power.setText(powerlv + strength);
		}
		Connect.clear();
		Game.updateboard();
		// opponentcardstacks.get(0)
		int id = getResources().getIdentifier("sample_0", "drawable",
				getPackageName());
		int index;
		clearopponentstacks();
		for (int i = 0; i < OpponentPlayer.stack1.size(); i++) {

			if (OpponentPlayer.stack1.get(i).cardID != 0) {
				switch (i) {

				case 0:

					ImageView img = (ImageView) findViewById(R.id.stack1card1);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack1.get(i),
							OpponentPlayer.stack1.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack1.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}

					break;

				case 1:

					img = (ImageView) findViewById(R.id.stack1card2);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack1.get(i),
							OpponentPlayer.stack1.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack1.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 2:

					img = (ImageView) findViewById(R.id.stack1card3);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack1.get(i),
							OpponentPlayer.stack1.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack1.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				}
			}
		}

		for (int i = 0; i < OpponentPlayer.stack2.size(); i++) {
			if (OpponentPlayer.stack2.get(i).cardID != 0) {
				switch (i) {

				case 0:

					ImageView img = (ImageView) findViewById(R.id.stack2card1);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack2.get(i),
							OpponentPlayer.stack2.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack2.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 1:

					img = (ImageView) findViewById(R.id.stack2card2);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack2.get(i),
							OpponentPlayer.stack2.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack2.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 2:

					img = (ImageView) findViewById(R.id.stack2card3);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack2.get(i),
							OpponentPlayer.stack2.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack2.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				}

			}
		}
		for (int i = 0; i < OpponentPlayer.stack3.size(); i++) {
			if (OpponentPlayer.stack3.get(i).cardID != 0) {
				switch (i) {

				case 0:

					ImageView img = (ImageView) findViewById(R.id.stack3card1);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack3.get(i),
							OpponentPlayer.stack3.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack3.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 1:

					img = (ImageView) findViewById(R.id.stack3card2);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack3.get(i),
							OpponentPlayer.stack3.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack3.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 2:

					img = (ImageView) findViewById(R.id.stack3card3);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack3.get(i),
							OpponentPlayer.stack3.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack3.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				}

			}
		}

		for (int i = 0; i < OpponentPlayer.stack4.size(); i++) {
			if (OpponentPlayer.stack4.get(i).cardID != 0) {
				switch (i) {

				case 0:

					ImageView img = (ImageView) findViewById(R.id.stack4card1);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack4.get(i),
							OpponentPlayer.stack4.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack4.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 1:

					img = (ImageView) findViewById(R.id.stack4card2);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack4.get(i),
							OpponentPlayer.stack4.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack4.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 2:

					img = (ImageView) findViewById(R.id.stack4card3);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack4.get(i),
							OpponentPlayer.stack4.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack4.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				}

			}
		}
		for (int i = 0; i < OpponentPlayer.stack5.size(); i++) {
			if (OpponentPlayer.stack5.get(i).cardID != 0) {
				switch (i) {

				case 0:

					ImageView img = (ImageView) findViewById(R.id.stack5card1);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack5.get(i),
							OpponentPlayer.stack5.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack5.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 1:

					img = (ImageView) findViewById(R.id.stack5card2);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack5.get(i),
							OpponentPlayer.stack5.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack5.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 2:

					img = (ImageView) findViewById(R.id.stack5card3);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack5.get(i),
							OpponentPlayer.stack5.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack5.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(90, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				}

			}
		}

	}
	void updateboard2() {
		try {
			Game.updateboard();
		} catch (JSONException e) {
			//
			e.printStackTrace();
		}
		// opponentcardstacks.get(0)
		int id = getResources().getIdentifier("sample_0", "drawable",
				getPackageName());
		int index;
		clearopponentstacks();
		for (int i = 0; i < OpponentPlayer.stack1.size(); i++) {

			if (OpponentPlayer.stack1.get(i).cardID != 0) {
				switch (i) {

				case 0:

					ImageView img = (ImageView) findViewById(R.id.stack1card1);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack1.get(i),
							OpponentPlayer.stack1.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack1.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}

					break;

				case 1:

					img = (ImageView) findViewById(R.id.stack1card2);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack1.get(i),
							OpponentPlayer.stack1.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack1.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 2:

					img = (ImageView) findViewById(R.id.stack1card3);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack1.get(i),
							OpponentPlayer.stack1.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack1.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				}
			}
		}

		for (int i = 0; i < OpponentPlayer.stack2.size(); i++) {
			if (OpponentPlayer.stack2.get(i).cardID != 0) {
				switch (i) {

				case 0:

					ImageView img = (ImageView) findViewById(R.id.stack2card1);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack2.get(i),
							OpponentPlayer.stack2.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack2.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 1:

					img = (ImageView) findViewById(R.id.stack2card2);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack2.get(i),
							OpponentPlayer.stack2.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack2.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 2:

					img = (ImageView) findViewById(R.id.stack2card3);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack2.get(i),
							OpponentPlayer.stack2.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack2.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				}

			}
		}
		for (int i = 0; i < OpponentPlayer.stack3.size(); i++) {
			if (OpponentPlayer.stack3.get(i).cardID != 0) {
				switch (i) {

				case 0:

					ImageView img = (ImageView) findViewById(R.id.stack3card1);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack3.get(i),
							OpponentPlayer.stack3.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack3.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 1:

					img = (ImageView) findViewById(R.id.stack3card2);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack3.get(i),
							OpponentPlayer.stack3.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack3.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 2:

					img = (ImageView) findViewById(R.id.stack3card3);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack3.get(i),
							OpponentPlayer.stack3.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack3.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				}

			}
		}

		for (int i = 0; i < OpponentPlayer.stack4.size(); i++) {
			if (OpponentPlayer.stack4.get(i).cardID != 0) {
				switch (i) {

				case 0:

					ImageView img = (ImageView) findViewById(R.id.stack4card1);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack4.get(i),
							OpponentPlayer.stack4.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack4.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 1:

					img = (ImageView) findViewById(R.id.stack4card2);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack4.get(i),
							OpponentPlayer.stack4.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack4.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 2:

					img = (ImageView) findViewById(R.id.stack4card3);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack4.get(i),
							OpponentPlayer.stack4.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack4.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				}

			}
		}
		for (int i = 0; i < OpponentPlayer.stack5.size(); i++) {
			if (OpponentPlayer.stack5.get(i).cardID != 0) {
				switch (i) {

				case 0:

					ImageView img = (ImageView) findViewById(R.id.stack5card1);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack5.get(i),
							OpponentPlayer.stack5.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack5.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 1:

					img = (ImageView) findViewById(R.id.stack5card2);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack5.get(i),
							OpponentPlayer.stack5.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack5.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				case 2:

					img = (ImageView) findViewById(R.id.stack5card3);
					recyclebitmap(img);
					img.setImageDrawable(writeOnDrawableReverse(id,
							OpponentPlayer.stack5.get(i),
							OpponentPlayer.stack5.get(i).cardID));
					img.setVisibility(View.VISIBLE);
					if (OpponentPlayer.stack5.get(i).tapped == true) {
						RotateAnimation anim2 = new RotateAnimation(0, 90,
								Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);

						anim2.setFillAfter(true);
						anim2.setDuration(100);
						img.startAnimation(anim2);
					}
					break;

				}

			}
		}
	}

	/*
	 * int canlevelup(CurrentCard card) { int count = 1; boolean oncetime;
	 * boolean cardonematch = false; boolean cardtwomatch = false; boolean
	 * cardthreematch = false; // iterate through resource stack by one for (int
	 * i = 0; i < 3; i++) { switch (i) {
	 * 
	 * case 0: // if resource card exists if (Player.stack1.get(i).cardID != 0)
	 * { // check the three possible resource types of // resource card 0 to see
	 * if it matches one of // the card's levelUp types if
	 * (!Player.stack1.get(i).type1.equals("")) { if
	 * (Player.stack1.get(i).type1.equals(card .getLevelUp1())) cardonematch =
	 * true; if (Player.stack1.get(i).type1.equals(card .getLevelUp1()))
	 * cardonematch = true; if (Player.stack1.get(i).type1.equals(card
	 * .getLevelUp3())) cardonematch = true; } if
	 * (!Player.stack1.get(i).type2.equals("")) { if
	 * (Player.stack1.get(i).type2.equals(card .getLevelUp1())) cardonematch =
	 * true; if (Player.stack1.get(i).type2.equals(card .getLevelUp1()))
	 * cardonematch = true; if (Player.stack1.get(i).type2.equals(card
	 * .getLevelUp3())) cardonematch = true; } if
	 * (!Player.stack1.get(i).type3.equals("")) { if
	 * (Player.stack1.get(i).type3.equals(card .getLevelUp1())) cardonematch =
	 * true; if (Player.stack1.get(i).type3.equals(card .getLevelUp1()))
	 * cardonematch = true; if (Player.stack1.get(i).type3.equals(card
	 * .getLevelUp3())) cardonematch = true; }
	 * 
	 * if (cardonematch) count++;
	 * 
	 * } break; case 1: // if resource card exists if
	 * (Player.stack1.get(i).cardID != 0) { // check the three possible resource
	 * types of // resource card 1 to see if it matches one of // the card's
	 * levelUp types if (!Player.stack1.get(i).type1.equals("")) { if
	 * (Player.stack1.get(i).type1.equals(card .getLevelUp1())) cardtwomatch =
	 * true; if (Player.stack1.get(i).type1.equals(card .getLevelUp1()))
	 * cardtwomatch = true; if (Player.stack1.get(i).type1.equals(card
	 * .getLevelUp3())) cardtwomatch = true; } if
	 * (!Player.stack1.get(i).type2.equals("")) { if
	 * (Player.stack1.get(i).type2.equals(card .getLevelUp1())) cardtwomatch =
	 * true; if (Player.stack1.get(i).type2.equals(card .getLevelUp1()))
	 * cardtwomatch = true; if (Player.stack1.get(i).type2.equals(card
	 * .getLevelUp3())) cardtwomatch = true; } if
	 * (!Player.stack1.get(i).type3.equals("")) { if
	 * (Player.stack1.get(i).type3.equals(card .getLevelUp1())) cardtwomatch =
	 * true; if (Player.stack1.get(i).type3.equals(card .getLevelUp1()))
	 * cardtwomatch = true; if (Player.stack1.get(i).type3.equals(card
	 * .getLevelUp3())) cardtwomatch = true; }
	 * 
	 * if (cardtwomatch) count++;
	 * 
	 * } break;
	 * 
	 * case 2: // if resource card exists if (Player.stack1.get(i).cardID != 0)
	 * { // check the three possible resource types of // resource card 2 to see
	 * if it matches one of // the card's levelUp types if
	 * (!Player.stack1.get(i).type1.equals("")) { if
	 * (Player.stack1.get(i).type1.equals(card .getLevelUp1())) cardthreematch =
	 * true; if (Player.stack1.get(i).type1.equals(card .getLevelUp1()))
	 * cardthreematch = true; if (Player.stack1.get(i).type1.equals(card
	 * .getLevelUp3())) cardthreematch = true; } if
	 * (!Player.stack1.get(i).type2.equals("")) { if
	 * (Player.stack1.get(i).type2.equals(card .getLevelUp1())) cardthreematch =
	 * true; if (Player.stack1.get(i).type2.equals(card .getLevelUp1()))
	 * cardthreematch = true; if (Player.stack1.get(i).type2.equals(card
	 * .getLevelUp3())) cardthreematch = true; } if
	 * (!Player.stack1.get(i).type3.equals("")) { if
	 * (Player.stack1.get(i).type3.equals(card .getLevelUp1())) cardthreematch =
	 * true; if (Player.stack1.get(i).type3.equals(card .getLevelUp1()))
	 * cardthreematch = true; if (Player.stack1.get(i).type3.equals(card
	 * .getLevelUp3())) cardthreematch = true; }
	 * 
	 * if (cardthreematch) count++;
	 * 
	 * } break;
	 * 
	 * } } if (count == 4) // can't be level4 count = 3; return count;
	 * 
	 * }
	 */

	// compares parameter card's levelUp to the resource types avail
	// and returns what level the card is.
	int canlevelup(CurrentCard card) {
		// each card starts at level 1
		int leveOfCard = 1;
		// iterate through resource stack by one
		for (int i = 0; i < Player.stack1.size(); i++) {
			// if resource card exists
			if (Player.stack1.get(i).cardID != 0) {
				// check the three possible levelUp types the card
				// against the three possible resource types of each card in
				// the stack
				if ((Player.stack1.get(i).type1.equals(card.getLevelUp1())
						|| Player.stack1.get(i).type1
								.equals(card.getLevelUp2()) || Player.stack1
							.get(i).type1.equals(card.getLevelUp3()))
						& !Player.stack1.get(i).type1.equals("")) {
					// increase level of card
					leveOfCard++;
				}
				if ((Player.stack1.get(i).type2.equals(card.getLevelUp1())
						|| Player.stack1.get(i).type2
								.equals(card.getLevelUp2()) || Player.stack1
							.get(i).type2.equals(card.getLevelUp3()))
						& !Player.stack1.get(i).type2.equals("")) {
					leveOfCard++;
				}
				if ((Player.stack1.get(i).type3.equals(card.getLevelUp1())
						|| Player.stack1.get(i).type3
								.equals(card.getLevelUp2()) || Player.stack1
							.get(i).type3.equals(card.getLevelUp3()))
						& !Player.stack1.get(i).type3.equals("")) {
					leveOfCard++;
				}
			}
		}
		// maximum level is 3
		if (leveOfCard == 4)
			leveOfCard = 3;
		return leveOfCard;
	}

	// takes the view id and card object and sets the stacks image to that
	// card's image
	void updateimage(int i, CurrentCard card) {
		// displays the first card on the stack
		ImageView stack = (ImageView) findViewById(i);
		int id = getResources().getIdentifier("sample_0", "drawable",
				getPackageName());
		try {
			recyclebitmap(stack);
			stack.setImageDrawable(writeOnDrawable(id, card, card.cardID));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// uses GamePlay.canlevelup() to get the level of the card
	// Player.stack#.CurrentCard.getLevel() to get card's level
	// Player.stack#.CurrentCard.setLevel() to update level if necessary
	// and GamePlay.updateimage() to setImageDrawable the leveled up card image
	void updatelevels() {
		for (int i = 0; i < Player.stack2.size(); i++) {
			if (Player.stack2.get(i).cardID != 0) {
				switch (i) {
				case 0:
					int lvl = canlevelup(Player.stack2.get(i));
					if (lvl > Player.stack2.get(i).getLevel()) {
						Player.stack2.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack2card1, Player.stack2.get(i));
					}

					if (lvl < Player.stack2.get(i).getLevel()) {
						Player.stack2.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack2card1, Player.stack2.get(i));
					}
					break;
				case 1:
					lvl = canlevelup(Player.stack2.get(i));
					if (lvl > Player.stack2.get(i).getLevel()) {
						Player.stack2.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack2card2, Player.stack2.get(i));
					}
					if (lvl < Player.stack2.get(i).getLevel()) {
						Player.stack2.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack2card2, Player.stack2.get(i));
					}
					break;
				case 2:
					lvl = canlevelup(Player.stack2.get(i));
					if (lvl > Player.stack2.get(i).getLevel()) {
						Player.stack2.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack2card3, Player.stack2.get(i));
					}
					if (lvl < Player.stack2.get(i).getLevel()) {
						Player.stack2.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack2card3, Player.stack2.get(i));
					}
					break;
				}

			}
		}
		for (int i = 0; i < Player.stack3.size(); i++) {
			if (Player.stack3.get(i).cardID != 0) {
				switch (i) {
				case 0:
					int lvl = canlevelup(Player.stack3.get(i));
					if (lvl > Player.stack3.get(i).getLevel()) {
						Player.stack3.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack3card1, Player.stack3.get(i));
					}
					if (lvl < Player.stack3.get(i).getLevel()) {
						Player.stack3.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack3card1, Player.stack3.get(i));
					}
					break;
				case 1:
					lvl = canlevelup(Player.stack3.get(i));
					if (lvl > Player.stack3.get(i).getLevel()) {
						Player.stack3.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack3card2, Player.stack3.get(i));
					}
					if (lvl < Player.stack3.get(i).getLevel()) {
						Player.stack3.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack3card2, Player.stack3.get(i));
					}
					break;
				case 2:
					lvl = canlevelup(Player.stack3.get(i));
					if (lvl > Player.stack3.get(i).getLevel()) {
						Player.stack3.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack3card3, Player.stack3.get(i));
					}
					if (lvl < Player.stack3.get(i).getLevel()) {
						Player.stack3.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack3card3, Player.stack3.get(i));
					}
					break;
				}

			}
		}

		for (int i = 0; i < Player.stack4.size(); i++) {
			if (Player.stack4.get(i).cardID != 0) {
				switch (i) {
				case 0:
					int lvl = canlevelup(Player.stack4.get(i));
					if (lvl > Player.stack4.get(i).getLevel()) {
						Player.stack4.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack4card1, Player.stack4.get(i));
					}
					if (lvl < Player.stack4.get(i).getLevel()) {
						Player.stack4.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack4card1, Player.stack4.get(i));
					}
					break;
				case 1:
					lvl = canlevelup(Player.stack4.get(i));
					if (lvl > Player.stack4.get(i).getLevel()) {
						Player.stack4.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack4card2, Player.stack4.get(i));
					}
					if (lvl < Player.stack4.get(i).getLevel()) {
						Player.stack4.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack4card2, Player.stack4.get(i));
					}
					break;
				case 2:
					lvl = canlevelup(Player.stack4.get(i));
					if (lvl > Player.stack4.get(i).getLevel()) {
						Player.stack4.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack4card3, Player.stack4.get(i));
					}
					if (lvl < Player.stack4.get(i).getLevel()) {
						Player.stack4.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack4card3, Player.stack4.get(i));
					}
					break;
				}

			}
		}

		for (int i = 0; i < Player.stack5.size(); i++) {
			if (Player.stack5.get(i).cardID != 0) {
				switch (i) {
				case 0:
					int lvl = canlevelup(Player.stack5.get(i));
					if (lvl > Player.stack5.get(i).getLevel()) {
						Player.stack5.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack5card1, Player.stack5.get(i));
					}
					if (lvl < Player.stack5.get(i).getLevel()) {
						Player.stack5.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack5card1, Player.stack5.get(i));
					}
					break;
				case 1:
					lvl = canlevelup(Player.stack5.get(i));
					if (lvl > Player.stack5.get(i).getLevel()) {
						Player.stack5.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack5card2, Player.stack5.get(i));
					}
					if (lvl < Player.stack5.get(i).getLevel()) {
						Player.stack5.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack5card2, Player.stack5.get(i));
					}
					break;
				case 2:
					lvl = canlevelup(Player.stack5.get(i));
					if (lvl > Player.stack5.get(i).getLevel()) {
						Player.stack5.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack5card3, Player.stack5.get(i));
					}
					if (lvl < Player.stack5.get(i).getLevel()) {
						Player.stack5.get(i).setlevel(String.valueOf(lvl));
						updateimage(R.id.mystack5card3, Player.stack5.get(i));
					}
					break;
				}

			}
		}

	}

	void getcardlibrarydata() throws JSONException {
		RestClient Connect = new RestClient(GamePlay.this);
		String mind1, charm1, strength1, mind2, charm2, strength2, mind3, charm3, strength3, cardname;
		List<String> type;
		List<String> levelup;
		String type1 = "";
		String type2 = "";
		String type3 = "";
		String levelup1 = "";
		String levelup2 = "";
		String levelup3 = "";
		String cost, stringtype, imgUri;
		int cardID;
		Tuple attr, types, levelups, mind, charm, strength;

		while (!Globals.libraryObj.getObjectState().equals("free")) {
			try { // to get cards in library
				if (Globals.libraryObj.getObjectState().equals(""))
					Globals.libraryObj.sqlGetLibrary();
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		// get initial card from stack

		String getmystackcard1 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + Player.getPlayer() + "stack5";
		Connect.connect(getmystackcard1);
		int checkconnect = Connect.findindex(Player.getPlayer() + "stack5");
		String mycardid;

		String stack;

		stack = Connect.getvalArray().getString(checkconnect);
		ArrayList<String> numbers = new ArrayList<String>(); // numbers from
																// card

		Pattern pp = Pattern.compile("\\d+"); // put numbers into an array that
												// has one with card id and
												// level
		Matcher m = pp.matcher(stack);
		while (m.find()) {
			numbers.add(m.group());
		}

		mycardid = numbers.get(0);

		Connect.clear();

		String gettheirstackcard1 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID()
				+ "&"
				+ OpponentPlayer.getPlayer()
				+ "stack5";
		Connect.connect(gettheirstackcard1);
		checkconnect = Connect.findindex(OpponentPlayer.getPlayer() + "stack5");

		String theircardid;

		if (checkconnect == -1
				|| Connect.getvalArray().getString(checkconnect).equals("null")) {
			gettheirstackcard1 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
					+ Game.getGameID()
					+ "&"
					+ OpponentPlayer.getPlayer()
					+ "stack1";
			Connect.connect(gettheirstackcard1);
			checkconnect = Connect.findindex(OpponentPlayer.getPlayer()
					+ "stack1");
			List<String> theircard = new ArrayList<String>();
			theircard = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));

			numbers = new ArrayList<String>(); // numbers from card

			pp = Pattern.compile("\\d+"); // put numbers into an array that has
											// one with card id and level
			m = pp.matcher(theircard.get(0));
			while (m.find()) {
				numbers.add(m.group());
			}

			theircardid = numbers.get(0);
		} else {
			numbers = new ArrayList<String>(); // numbers from card

			pp = Pattern.compile("\\d+"); // put numbers into an array that has
											// one with card id and level
			m = pp.matcher(Connect.getvalArray().getString(checkconnect));
			while (m.find()) {
				numbers.add(m.group());
			}

			theircardid = numbers.get(0);

		}

		Connect.clear();

		// Establish connection to my play deck
		String getmydeck = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + Player.getPlayer() + "playDeck";
		Connect.connect(getmydeck);

		checkconnect = Connect.findindex(Player.getPlayer() + "playDeck");
		List<String> mydeck = new ArrayList<String>();

		if (checkconnect != -1)
			mydeck = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));

		Connect.clear();

		mydeck.add(0, mycardid);
		mydeck.add(1, theircardid);

		// Read-in Cards from MyDeck (1 at a time)
		for (int i = 0; i < mydeck.size(); i++) {
			// Establish Connection to my card
			Card tempcard = Globals.libraryObj.getCardbyID(mydeck.get(i));
			// reset fields
			type1 = "";
			type2 = "";
			type3 = "";
			levelup1 = "";
			levelup2 = "";
			levelup3 = "";
			// Read-in Data Fields
			imgUri = tempcard.getImage();
			cardID = Integer.parseInt(mydeck.get(i));
			mind1 = tempcard.getMind1();
			charm1 = tempcard.getCharm1();
			strength1 = tempcard.getStrength1();
			mind2 = tempcard.getMind2();
			charm2 = tempcard.getCharm2();
			strength2 = tempcard.getStrength2();
			mind3 = tempcard.getMind3();
			charm3 = tempcard.getCharm3();
			strength3 = tempcard.getStrength3();
			stringtype = tempcard.getType();
			type = Arrays.asList(stringtype.split("\\s*:\\s*"));
			cost = tempcard.getCost();
			stringtype = tempcard.getLevelUp();
			levelup = Arrays.asList(stringtype.split("\\s*:\\s*"));
			cardname = tempcard.getCardName();
			// Create multiple types (if they exist)
			for (int y = 0; y < type.size(); y++) {
				switch (y) {
				case 0:
					type1 = type.get(y);
					break;
				case 1:
					type2 = type.get(y);
					break;
				case 2:
					type3 = type.get(y);
					break;
				}
			}
			// Create multiple levelups
			for (int y = 0; y < levelup.size(); y++) {
				switch (y) {
				case 0:
					levelup1 = levelup.get(y);
					break;
				case 1:
					levelup2 = levelup.get(y);
					break;
				case 2:
					levelup3 = levelup.get(y);
					break;
				}
			}

			// Create Card
			attr = new Tuple(mind1, charm1, strength1);
			types = new Tuple(type1, type2, type3);
			levelups = new Tuple(levelup1, levelup2, levelup3);
			mind = new Tuple(mind1, mind2, mind3);
			charm = new Tuple(charm1, charm2, charm3);
			strength = new Tuple(strength1, strength2, strength3);
			CurrentCard card = new CurrentCard(cardID, attr, types, cost,
					levelups, mind, charm, strength, cardname);
			card.setlevel("1");

			// Add card to CardLibrary
			CardLibrary.cardlibrary.set(cardID, card);

			// Add card to players deck
			if (i != 0 && i != 1)
				Player.mydeck.add(card);

			Bitmaparray.CardurlArray.add(imgUri);
			Connect.clear();

		}

		/*** REPEAT PROCESS FOR OPPONENTS DECK ***/

		// Establish connection to opponents play deck
		String getopponentdeck = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID()
				+ "&"
				+ OpponentPlayer.getPlayer()
				+ "playDeck";
		Connect.connect(getopponentdeck);

		checkconnect = Connect.findindex(OpponentPlayer.getPlayer()
				+ "playDeck");
		List<String> opposingplayerdeck = null;

		if (checkconnect != -1)
			opposingplayerdeck = Game.parsecards(Connect.valArray
					.getString(checkconnect));

		Connect.clear();

		// Read-in Cards from MyOpponentsDeck (1 at a time)
		for (int i = 0; i < opposingplayerdeck.size(); i++) {
			// Establish Connection to my opponents card
			Card tempcard = Globals.libraryObj.getCardbyID(opposingplayerdeck
					.get(i));
			// reset
			type1 = "";
			type2 = "";
			type3 = "";
			levelup1 = "";
			levelup2 = "";
			levelup3 = "";
			// Read-in Data Fields
			imgUri = tempcard.getImage();
			cardID = Integer.parseInt(opposingplayerdeck.get(i));
			mind1 = tempcard.getMind1();
			charm1 = tempcard.getCharm1();
			strength1 = tempcard.getStrength1();
			mind2 = tempcard.getMind2();
			charm2 = tempcard.getCharm2();
			strength2 = tempcard.getStrength2();
			mind3 = tempcard.getMind3();
			charm3 = tempcard.getCharm3();
			strength3 = tempcard.getStrength3();
			stringtype = tempcard.getType();
			type = Arrays.asList(stringtype.split("\\s*:\\s*"));
			cost = tempcard.getCost();
			stringtype = tempcard.getLevelUp();
			levelup = Arrays.asList(stringtype.split("\\s*:\\s*"));
			cardname = tempcard.getCardName();
			// Create multiple types (if they exist)
			for (int y = 0; y < type.size(); y++) {
				switch (y) {
				case 0:
					type1 = type.get(y);
					break;
				case 1:
					type2 = type.get(y);
					break;
				case 2:
					type3 = type.get(y);
					break;
				}
			}
			// Create multiple levelups
			for (int y = 0; y < levelup.size(); y++) {
				switch (y) {
				case 0:
					levelup1 = levelup.get(y);
					break;
				case 1:
					levelup2 = levelup.get(y);
					break;
				case 2:
					levelup3 = levelup.get(y);
					break;
				}
			}
			// Create Card
			attr = new Tuple(mind1, charm1, strength1);
			types = new Tuple(type1, type2, type3);
			levelups = new Tuple(levelup1, levelup2, levelup3);
			mind = new Tuple(mind1, mind2, mind3);
			charm = new Tuple(charm1, charm2, charm3);
			strength = new Tuple(strength1, strength2, strength3);
			CurrentCard card = new CurrentCard(cardID, attr, types, cost,
					levelups, mind, charm, strength, cardname);
			card.setlevel("1");

			// Add card to CardLibrary
			CardLibrary.cardlibrary.set(cardID, card);
			// Add card to CardLibrary
			// CardLibrary.cardlibrary.set(cardID, card);
			// Check if card is already in download queue
			boolean found = false;
			for (int k = 0; k < Bitmaparray.CardurlArray.size(); k++)
				if (Bitmaparray.CardurlArray.get(k).equals(imgUri)) {
					found = true;
					break;
				}
			if (!found)
				Bitmaparray.CardurlArray.add(imgUri);
			Connect.clear();

		}
	}
	/*
	 * void downloadcardlibrary() { DisplayImageOptions defaultOptions = new
	 * DisplayImageOptions.Builder()
	 * .bitmapConfig(Bitmap.Config.RGB_565).build(); ImageLoaderConfiguration
	 * config = new ImageLoaderConfiguration.Builder( getApplicationContext())
	 * .enableLogging() .threadPoolSize(5) .memoryCache(new WeakMemoryCache())
	 * .memoryCacheExtraOptions(250,300 )
	 * .defaultDisplayImageOptions(defaultOptions) .imageDownloader( new
	 * BaseImageDownloader(getApplicationContext(), 20000, 30000)).build();
	 * ImageLoader.getInstance().init(config);
	 * 
	 * for (int i = 0; i < Bitmaparray.CardurlArray.size(); i++) {
	 * 
	 * if (Bitmaparray.CardurlArray.get(i) != "0") { String imgUri =
	 * Bitmaparray.CardurlArray.get(i);
	 * ImageLoader.getInstance().loadImage(imgUri, new
	 * SimpleImageLoadingListener() {
	 * 
	 * @Override public void onLoadingComplete(String imageUri, View view,
	 * Bitmap loadedImage) { int i = Integer.parseInt(imageUri.replaceAll(
	 * "\\D", "")); final float scale =
	 * getResources().getDisplayMetrics().density; int width = (int) (120 *
	 * scale + 0.5f); int height=(int) (133.33 * scale + 0.5f); DisplayMetrics
	 * metrics = getResources().getDisplayMetrics();
	 * 
	 * CardLibrary.cardlibrary.get(i).bitmap =
	 * Bitmap.createScaledBitmap(loadedImage, width,height, false);
	 * Counter.incrementCount(); Log.e("test", imageUri);
	 * 
	 * //
	 * img2.setImageBitmap(Bitmaparray.bitmapConnect.get(Bitmaparray.bitmapConnect
	 * .size()-1)); // int id = // getResources().getIdentifier("sample_0", //
	 * "drawable", getPackageName());
	 * 
	 * // img2.setImageDrawable(writeOnDrawable(id,"test")); //
	 * img2.setVisibility(View.VISIBLE);
	 * 
	 * } });
	 * 
	 * } }
	 * 
	 * }
	 */

	void initboard() throws JSONException, IOException {
		RestClient Connect = new RestClient(GamePlay.this);
		// game can no longer be found by find game
		String setround = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID=" // game
				+ Game.getGameID() + "&round=ingame";

		Connect.connect(setround);
		// downloadcardlibrary();
		ImageView Deck = (ImageView) findViewById(R.id.deck);
		Deck.setVisibility(View.VISIBLE);
		Player.drawinitialhand();

		ImageView img = (ImageView) findViewById(R.id.myhandcard1);
		int id = getResources().getIdentifier("sample_0", "drawable",
				getPackageName());
		recyclebitmap(img);
		img.setImageDrawable(writeOnDrawable(id, Player.myhand.get(0),
				Player.myhand.get(0).cardID));
		img.setVisibility(View.VISIBLE);

		ImageView img2 = (ImageView) findViewById(R.id.myhandcard2);
		id = getResources().getIdentifier("sample_0", "drawable",
				getPackageName());
		recyclebitmap(img2);
		img2.setImageDrawable(writeOnDrawable(id, Player.myhand.get(1),
				Player.myhand.get(1).cardID));
		img2.setVisibility(View.VISIBLE);

		ImageView img3 = (ImageView) findViewById(R.id.myhandcard3);
		id = getResources().getIdentifier("sample_0", "drawable",
				getPackageName());
		recyclebitmap(img3);
		img3.setImageDrawable(writeOnDrawable(id, Player.myhand.get(2),
				Player.myhand.get(2).cardID));
		img3.setVisibility(View.VISIBLE);

		ImageView img4 = (ImageView) findViewById(R.id.myhandcard4);
		id = getResources().getIdentifier("sample_0", "drawable",
				getPackageName());
		recyclebitmap(img4);
		img4.setImageDrawable(writeOnDrawable(id, Player.myhand.get(3),
				Player.myhand.get(3).cardID));
		img4.setVisibility(View.VISIBLE);

		ImageView img5 = (ImageView) findViewById(R.id.myhandcard5);
		id = getResources().getIdentifier("sample_0", "drawable",
				getPackageName());
		recyclebitmap(img5);
		img5.setImageDrawable(writeOnDrawable(id, Player.myhand.get(4),
				Player.myhand.get(4).cardID));
		img5.setVisibility(View.VISIBLE);

		ImageView img6 = (ImageView) findViewById(R.id.myhandcard6);
		id = getResources().getIdentifier("sample_0", "drawable",
				getPackageName());
		recyclebitmap(img6);
		img6.setImageDrawable(writeOnDrawable(id, Player.myhand.get(5),
				Player.myhand.get(5).cardID));
		img6.setVisibility(View.VISIBLE);

		String getmystackcard1 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + Player.getPlayer() + "stack5";
		Connect.connect(getmystackcard1);
		int checkconnect = Connect.findindex(Player.getPlayer() + "stack5");
		int mycardid;
		String stack;

		stack = Connect.getvalArray().getString(checkconnect);
		ArrayList<String> numbers = new ArrayList<String>(); // numbers from
																// card

		Pattern pp = Pattern.compile("\\d+"); // put numbers into an array that
												// has one with card id and
												// level
		Matcher m = pp.matcher(stack);
		while (m.find()) {
			numbers.add(m.group());
		}

		mycardid = Integer.parseInt(numbers.get(0));

		// mycardid=Integer.parseInt(Connect.getvalArray().getString(checkconnect));

		Connect.clear();
		if (CardLibrary.cardlibrary.get(mycardid).cardType.equals("Resource")) {
			CurrentCard card = new CurrentCard(
					CardLibrary.cardlibrary.get(mycardid));
			Player.stack1.set(0, card);
			Player.stack1.get(0).setlevel(numbers.get(1));
			// this is to display the first card on the stack
			ImageView stack1 = (ImageView) findViewById(R.id.mystack1card1);
			id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			recyclebitmap(stack1);
			stack1.setImageDrawable(writeOnDrawable(id, Player.stack1.get(0),
					Player.stack1.get(0).cardID));
			stack1.setVisibility(View.VISIBLE);
			String setstackcard1 = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
					+ Game.getGameID()
					+ "&"
					+ Player.getPlayer()
					+ "stack1="
					+ String.valueOf(mycardid) + "lvl" + "1";
			Connect.connect(setstackcard1);
			String setstackcard5 = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
					+ Game.getGameID()
					+ "&"
					+ Player.getPlayer()
					+ "stack5=null";
			Connect.connect(setstackcard5);

		} else {
			CurrentCard card = new CurrentCard(
					CardLibrary.cardlibrary.get(mycardid));
			Player.stack5.set(0, card);
			Player.stack5.get(0).setlevel(numbers.get(1));
			// this is to display the first card on the stack
			ImageView stack5 = (ImageView) findViewById(R.id.mystack5card1);
			id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			recyclebitmap(stack5);
			stack5.setImageDrawable(writeOnDrawable(id, Player.stack5.get(0),
					Player.stack5.get(0).cardID));
			stack5.setVisibility(View.VISIBLE);
		}

		String gettheirstackcard1 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID()
				+ "&"
				+ OpponentPlayer.getPlayer()
				+ "stack5";
		Connect.connect(gettheirstackcard1);
		checkconnect = Connect.findindex(OpponentPlayer.getPlayer() + "stack5");
		int theircardid;
		if (checkconnect == -1
				|| Connect.getvalArray().getString(checkconnect).equals("null")) {
			gettheirstackcard1 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
					+ Game.getGameID()
					+ "&"
					+ OpponentPlayer.getPlayer()
					+ "stack1";
			Connect.connect(gettheirstackcard1);
			checkconnect = Connect.findindex(OpponentPlayer.getPlayer()
					+ "stack1");
			List<String> theircard = new ArrayList<String>();
			theircard = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));

			numbers = new ArrayList<String>(); // numbers from card

			pp = Pattern.compile("\\d+"); // put numbers into an array that has
											// one with card id and level
			m = pp.matcher(theircard.get(0));
			while (m.find()) {
				numbers.add(m.group());
			}

			theircardid = Integer.parseInt(numbers.get(0));
		} else {
			numbers = new ArrayList<String>(); // numbers from card

			pp = Pattern.compile("\\d+"); // put numbers into an array that has
											// one with card id and level
			m = pp.matcher(Connect.getvalArray().getString(checkconnect));
			while (m.find()) {
				numbers.add(m.group());
			}

			theircardid = Integer.parseInt(numbers.get(0));

		}

		Connect.clear();

		TextView theirpower = (TextView) findViewById(R.id.theirpower);
		String theirpowerlv = "Foe Strength: ";
		theirpower.setText(theirpowerlv
				+ Game.toFraction(CardLibrary.cardlibrary.get(theircardid)
						.getPower().doubleValue(), 1000));

		if (CardLibrary.cardlibrary.get(mycardid).getPower().doubleValue() > CardLibrary.cardlibrary
				.get(theircardid).getPower().doubleValue()) {

			Player.donewaiting = false;
			task = new WaitTurn();
			task.execute(getApplicationContext());
			//loopupdateboard();

		} else if (CardLibrary.cardlibrary.get(mycardid).getPower()
				.doubleValue() == CardLibrary.cardlibrary.get(theircardid)
				.getPower().doubleValue()) {

			if (!Game.isCreatedgame()) {

				Player.donewaiting = false;
				task = new WaitTurn();
				task.execute(getApplicationContext());
				//loopupdateboard();

			} else {
				String turn = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
						+ Game.getGameID() + "&turn=" + Player.getPlayer();
				Connect.connect(turn);
				Connect.clear();
				//task2 = new UpdateGame();
				//task2.execute(getApplicationContext());

			}
		} else if (CardLibrary.cardlibrary.get(mycardid).getPower()
				.doubleValue() < CardLibrary.cardlibrary.get(theircardid)
				.getPower().doubleValue()) {
			String turn = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
					+ Game.getGameID() + "&turn=" + Player.getPlayer();
			Connect.connect(turn);
			Connect.clear();
		//	task2 = new UpdateGame();
		//	task2.execute(getApplicationContext());
		}

		TextView numcards = (TextView) findViewById(R.id.numcards);
		String number = "";
		numcards.setText(number + String.valueOf(Player.mydeck.size()));

		updateboard();

		// = new UpdateGame();
		// task2.execute(getApplicationContext());

		updatestrength();

	}

	void Downloadcards() {

		new DownloadCards().execute(GamePlay.this);
	}
	int getmypercenthealth() {
		return (int) ((Player.health.doubleValue() / 20.0) * 100);
	}
	int getopponentpercenthealth() {
		return (int) ((OpponentPlayer.health.doubleValue() / 20.0) * 100);
	}
	private ProgressBar progressbar;
	private ProgressBar progressbar2;
	private int mProgressStatus = 100;
	private int mProgressStatus2 = 100;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
			//logHeap();
			setContentView(R.layout.gameplay);

			Intent in = new Intent(getApplicationContext(),
					EnterUsername.class);
			Game.intent = PendingIntent.getActivity(getApplicationContext(), 0,
					in, in.getFlags());
			Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(
					GamePlay.this, GamePlay.this));

			progressbar = (ProgressBar) findViewById(R.id.progressbar);
			progressbar2 = (ProgressBar) findViewById(R.id.progressbar2);
			TextView myhealth = (TextView) findViewById(R.id.theirhealth); // I
																			// know
																			// this
																			// is
																			// reversed.

			Player.endturn = false; // It is
			Game.exit = false;// a bug

			new Thread(new Runnable() {
				public void run() {
					while (mProgressStatus > 0 && !Game.exit) {
						mProgressStatus = getmypercenthealth();

						// Update the progress bar
						mHandler.post(new Runnable() {
							public void run() {
								progressbar.setProgress(mProgressStatus);
							}
						});
					}
				}
			}).start();

			new Thread(new Runnable() {
				public void run() {
					while (mProgressStatus2 > 0 && !Game.exit) {
						mProgressStatus2 = getopponentpercenthealth();

						// Update the progress bar
						mHandler.post(new Runnable() {
							public void run() {
								progressbar2.setProgress(mProgressStatus2);
							}
						});
					}
				}
			}).start();
			

			String myhealthlv = "My Points: ";
			myhealth.setText(myhealthlv
					+ Game.toFraction(Player.health.doubleValue(), 1000));

			TextView theirhealth = (TextView) findViewById(R.id.myhealth);
			String healthlv = "Foe Points: ";
			theirhealth
					.setText(healthlv
							+ Game.toFraction(
									OpponentPlayer.health.doubleValue(), 1000));
			// new DownloadCards().execute(getApplicationContext());
			Bitmaparray.CardurlArray = new ArrayList<String>();
			final RestClient Connect = new RestClient(GamePlay.this);
			Player.mydeck = new ArrayList<CurrentCard>();
			CardLibrary.cardlibrary = new ArrayList<CurrentCard>();
			Bitmaparray.bitmapArray = new ArrayList<Bitmap>();
			Player.myhand = new ArrayList<CurrentCard>(6);
			Player.stack1 = new ArrayList<CurrentCard>();
			Player.stack2 = new ArrayList<CurrentCard>();
			Player.stack3 = new ArrayList<CurrentCard>();
			Player.stack4 = new ArrayList<CurrentCard>();
			Player.stack5 = new ArrayList<CurrentCard>();
			OpponentPlayer.stack1 = new ArrayList<CurrentCard>();
			OpponentPlayer.stack2 = new ArrayList<CurrentCard>();
			OpponentPlayer.stack3 = new ArrayList<CurrentCard>();
			OpponentPlayer.stack4 = new ArrayList<CurrentCard>();
			OpponentPlayer.stack5 = new ArrayList<CurrentCard>();

			for (int i = 0; i < 100; i++) {
				CurrentCard card = new CurrentCard(0);
				CardLibrary.cardlibrary.add(card);
			}
			for (int i = 0; i < 3; i++) {
				CurrentCard card = new CurrentCard(0);
				Player.stack1.add(card);
				Player.stack2.add(card);
				Player.stack3.add(card);
				Player.stack4.add(card);
				Player.stack5.add(card);

				OpponentPlayer.stack1.add(card);
				OpponentPlayer.stack2.add(card);
				OpponentPlayer.stack3.add(card);
				OpponentPlayer.stack4.add(card);
				OpponentPlayer.stack5.add(card);
			}

			final ImageView myhandcard1 = (ImageView) findViewById(R.id.myhandcard1);
			final ImageView myhandcard2 = (ImageView) findViewById(R.id.myhandcard2);
			final ImageView myhandcard3 = (ImageView) findViewById(R.id.myhandcard3);
			final ImageView myhandcard4 = (ImageView) findViewById(R.id.myhandcard4);
			final ImageView myhandcard5 = (ImageView) findViewById(R.id.myhandcard5);
			final ImageView myhandcard6 = (ImageView) findViewById(R.id.myhandcard6);
			final ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image);
			final Button endturn = (Button) findViewById(R.id.endturn);
			final Button challenge = (Button) findViewById(R.id.challenge);

			endturn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) { 
					
					task3=new EndTurn();
					task3.execute(getApplicationContext());
					
					endturn.setEnabled(false);
					challenge.setEnabled(false);
					
					//loopupdateboard();

				}

			});
			challenge.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					try {
						endturn.setEnabled(false);
						challenge.setEnabled(false);
						Player.challenger = true;
						challenge(true);
						turnallcards();
						String otherplayer;

						Player.endturn = true;
						String p = Player.getPlayer();
						if (Player.challenged) {
							String sendstrength = "http://emath-project.appspot.com/emath/emathservicehttp?function=setaction&gameID="
									+ Game.getGameID()
									+ "&"
									+ "round=challenge"
									+ "&"
									+ "turn="
									+ Player.getPlayer()
									+ "&"
									+ "action="
									+ Game.getbeforechallengestring();
							Connect.connect(sendstrength);
							Connect.clear();
							Player.challenged = false;
						} else {
							String sendhealth = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
									+ Game.getGameID()
									+ "&"
									+ Player.getPlayer()
									+ "power="
									+ Game.getmyhealthstring();
							Connect.connect(sendhealth);
							Connect.clear();

							String sendstrength = "http://emath-project.appspot.com/emath/emathservicehttp?function=setaction&gameID="
									+ Game.getGameID()
									+ "&"
									+ "round=challenge"
									+ "&"
									+ "turn="
									+ Player.getPlayer()
									+ "&"
									+ "action="
									+ Game.getmypowerlevelstring();
							Connect.connect(sendstrength);
							Connect.clear();
						}

						// endgame(true);
						// new WaitChallenge().execute(getApplicationContext());
					} catch (JSONException e) {
						//
						e.printStackTrace();
					}

					// new WaitTurn().execute(getApplicationContext());
				}

			});
			myhandcard1.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					CharSequence[] array = { "Place Card On Field", "Discard",
							"X" };
					// img.set
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					Drawable drawable = zoomcard.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}

					// img2.setImageResource(id);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.myhand.get(0),
								Player.myhand.get(0).cardID));
					} catch (IOException e1) {
						e1.printStackTrace();
					}

					zoomcard.setVisibility(View.VISIBLE);
					// ContextThemeWrapper cw = new ContextThemeWrapper(
					// GamePlay.this, R.style.AlertDialogTheme );

					AlertDialog.Builder builder = new AlertDialog.Builder(
							GamePlay.this);

					SpannableStringBuilder ssBuilser = new SpannableStringBuilder(
							"Choose An Action");
					// StyleSpan span = new StyleSpan(Typeface.ITALIC);
					ScaleXSpan span1 = new ScaleXSpan(15);
					ssBuilser.setSpan(new AbsoluteSizeSpan(20), 0,
							ssBuilser.length(),
							Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

					builder.setTitle(ssBuilser);

					ArrayAdapter<CharSequence> itemsAdapter = new ArrayAdapter<CharSequence>(
							GamePlay.this, R.layout.menu_items, array);

					builder.setAdapter(itemsAdapter,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									switch (which) {

									case 0:
										if (!Player.canplayresource
												&& Player.myhand.get(0).cardType
														.equals("Resource")) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already placed one resource down")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {
																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										int find = Game
												.findposition(Player.myhand
														.get(0));
										if (find == -1) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Not enough Resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -2) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already have 3 resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -3) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Max cards on board")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}

										ImageView onboard = (ImageView) findViewById(find);
										int id = getResources().getIdentifier(
												"sample_0", "drawable",
												getPackageName());

										try {
											recyclebitmap(onboard);
											onboard.setImageDrawable(writeOnDrawable(
													id, Player.myhand.get(0),
													Player.myhand.get(0).cardID));
										} catch (IOException e) {
											e.printStackTrace();
										}
										onboard.setVisibility(View.VISIBLE);
										// if(!Player.myhand.get(0).type.equals("University")&&!Player.myhand.get(0).type.equals("University")&&!Player.myhand.get(0).type.equals("")&&!Player.myhand.get(0).type.equals("University"))
										turncards(Player.myhand.get(0));
										myhandcard1
												.setVisibility(View.INVISIBLE);

										if (Player.myhand.get(0).cardType
												.equals("Character")) { // make
																		// player
																		// submit
																		// power
											Player.currentcardpower = Player.myhand
													.get(0).getPower();
											Player.currentcard = Player.myhand
													.get(0);
											Intent intent = new Intent(
													GamePlay.this,
													FractionInput.class);
											Globals.focusedActivity = "FractionInput";
											startActivity(intent);
											new WaitForInput()
													.execute(getApplicationContext());

										}
										updatelevels();
										if (Player.myhand.get(0).cardType
												.equals("Resource"))
											updatestrength();
										CurrentCard card = new CurrentCard(0);
										Player.myhand.set(0, card);
										break;

									// The 'which' argument contains the index
									// position
									// of the selected item
									case 1:
										new AlertDialog.Builder(GamePlay.this)
												.setTitle("Really Discard?")
												.setMessage(
														"Are you sure you want to discard this card")
												.setNegativeButton(
														android.R.string.no,
														null)
												.setPositiveButton(
														android.R.string.yes,
														new DialogInterface.OnClickListener() {

															public void onClick(
																	DialogInterface arg0,
																	int arg1) {
																if (Player.candiscard) {
																	ImageView image = (ImageView) findViewById(R.id.myhandcard1);
																	image.setVisibility(View.INVISIBLE);
																	CurrentCard nocard = new CurrentCard(
																			0);
																	Player.myhand
																			.set(0,
																					nocard);

																	Player.candiscard = false;

																} else {
																	AlertDialog.Builder builder = new AlertDialog.Builder(
																			GamePlay.this);
																	builder.setMessage(
																			"You have already discarded 1 card")
																			.setNegativeButton(
																					"Ok",
																					new DialogInterface.OnClickListener() {
																						@Override
																						public void onClick(
																								DialogInterface dialog,
																								int id) {

																							// User
																							// cancelled
																							// the
																							// dialog
																						}
																					});
																	// Create
																	// the
																	// AlertDialog
																	// object
																	// and
																	// return it
																	builder.create();
																	builder.show();
																}
															}
														}).create().show();

										break;
									}
								}

							});

					AlertDialog alertDialog = builder.create();
					alertDialog.show();

					WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

					lp.copyFrom(alertDialog.getWindow().getAttributes());
					lp.width = 250;
					lp.height = 500;
					lp.x = 170;
					lp.y = 100;
					alertDialog.getWindow().setAttributes(lp);

				}
			});
			myhandcard2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					CharSequence[] array = { "Place Card On Field", "Discard",
							"X" };

					// img.set
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());

					// img2.setImageResource(id);
					Drawable drawable = zoomcard.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.myhand.get(1),
								Player.myhand.get(1).cardID));
					} catch (IOException e) {
						e.printStackTrace();
					}

					zoomcard.setVisibility(View.VISIBLE);
					// ContextThemeWrapper cw = new ContextThemeWrapper(
					// GamePlay.this, R.style.AlertDialogTheme );

					AlertDialog.Builder builder = new AlertDialog.Builder(
							GamePlay.this);

					SpannableStringBuilder ssBuilser = new SpannableStringBuilder(
							"Choose An Action");
					// StyleSpan span = new StyleSpan(Typeface.ITALIC);
					ScaleXSpan span1 = new ScaleXSpan(15);
					ssBuilser.setSpan(new AbsoluteSizeSpan(20), 0,
							ssBuilser.length(),
							Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

					builder.setTitle(ssBuilser);

					ArrayAdapter<CharSequence> itemsAdapter = new ArrayAdapter<CharSequence>(
							GamePlay.this, R.layout.menu_items, array);

					builder.setAdapter(itemsAdapter,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									switch (which) {

									case 0:
										if (!Player.canplayresource
												&& Player.myhand.get(1).cardType
														.equals("Resource")) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already placed one resource down")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {
																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										int find = Game
												.findposition(Player.myhand
														.get(1));
										if (find == -1) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Not enough Resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {
																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -2) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already have 3 resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -3) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Max cards on board")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										ImageView onboard = (ImageView) findViewById(find);
										int id = getResources().getIdentifier(
												"sample_0", "drawable",
												getPackageName());

										try {
											recyclebitmap(onboard);
											onboard.setImageDrawable(writeOnDrawable(
													id, Player.myhand.get(1),
													Player.myhand.get(1).cardID));
										} catch (IOException e) {
											e.printStackTrace();
										}
										onboard.setVisibility(View.VISIBLE);
										turncards(Player.myhand.get(1));
										myhandcard2
												.setVisibility(View.INVISIBLE);

										if (Player.myhand.get(1).cardType
												.equals("Character")) { // make
																		// player
																		// submit
																		// power
											Player.currentcardpower = Player.myhand
													.get(1).getPower();
											Player.currentcard = Player.myhand
													.get(1);
											Intent intent = new Intent(
													GamePlay.this,
													FractionInput.class);
											Globals.focusedActivity = "FractionInput";
											startActivity(intent);
											new WaitForInput()
													.execute(getApplicationContext());

										}
										updatelevels();
										if (Player.myhand.get(1).cardType
												.equals("Resource"))
											updatestrength();
										CurrentCard card = new CurrentCard(0);
										Player.myhand.set(1, card);
										break;
									// The 'which' argument contains the index
									// position
									// of the selected item
									case 1:
										new AlertDialog.Builder(GamePlay.this)
												.setTitle("Really Discard?")
												.setMessage(
														"Are you sure you want to discard this card")
												.setNegativeButton(
														android.R.string.no,
														null)
												.setPositiveButton(
														android.R.string.yes,
														new DialogInterface.OnClickListener() {

															public void onClick(
																	DialogInterface arg0,
																	int arg1) {
																if (Player.candiscard) {
																	ImageView image = (ImageView) findViewById(R.id.myhandcard2);
																	image.setVisibility(View.INVISIBLE);
																	CurrentCard nocard = new CurrentCard(
																			0);
																	Player.myhand
																			.set(1,
																					nocard);

																	Player.candiscard = false;

																} else {
																	AlertDialog.Builder builder = new AlertDialog.Builder(
																			GamePlay.this);
																	builder.setMessage(
																			"You have already discarded 1 card")
																			.setNegativeButton(
																					"Ok",
																					new DialogInterface.OnClickListener() {
																						@Override
																						public void onClick(
																								DialogInterface dialog,
																								int id) {

																							// User
																							// cancelled
																							// the
																							// dialog
																						}
																					});
																	// Create
																	// the
																	// AlertDialog
																	// object
																	// and
																	// return it
																	builder.create();
																	builder.show();
																}
															}
														}).create().show();

										break;

									}
								}

							});

					AlertDialog alertDialog = builder.create();
					alertDialog.show();

					WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

					lp.copyFrom(alertDialog.getWindow().getAttributes());
					lp.width = 250;
					lp.height = 500;
					lp.x = 170;
					lp.y = 100;
					alertDialog.getWindow().setAttributes(lp);

				}
			});
			myhandcard3.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					CharSequence[] array = { "Place Card On Field", "Discard",
							"X" };

					// img.set
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					Drawable drawable = zoomcard.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}

					// img2.setImageResource(id);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.myhand.get(2),
								Player.myhand.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}

					zoomcard.setVisibility(View.VISIBLE);
					// ContextThemeWrapper cw = new ContextThemeWrapper(
					// GamePlay.this, R.style.AlertDialogTheme );

					AlertDialog.Builder builder = new AlertDialog.Builder(
							GamePlay.this);

					SpannableStringBuilder ssBuilser = new SpannableStringBuilder(
							"Choose An Action");
					// StyleSpan span = new StyleSpan(Typeface.ITALIC);
					ScaleXSpan span1 = new ScaleXSpan(15);
					ssBuilser.setSpan(new AbsoluteSizeSpan(20), 0,
							ssBuilser.length(),
							Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

					builder.setTitle(ssBuilser);

					ArrayAdapter<CharSequence> itemsAdapter = new ArrayAdapter<CharSequence>(
							GamePlay.this, R.layout.menu_items, array);

					builder.setAdapter(itemsAdapter,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									switch (which) {

									case 0:
										if (!Player.canplayresource
												&& Player.myhand.get(2).cardType
														.equals("Resource")) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already placed one resource down")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {
																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										int find = Game
												.findposition(Player.myhand
														.get(2));
										if (find == -1) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Not enough Resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {
																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -2) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already have 3 resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -3) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Max cards on board")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										ImageView onboard = (ImageView) findViewById(find);
										int id = getResources().getIdentifier(
												"sample_0", "drawable",
												getPackageName());

										try {
											recyclebitmap(onboard);
											onboard.setImageDrawable(writeOnDrawable(
													id, Player.myhand.get(2),
													Player.myhand.get(2).cardID));
										} catch (IOException e) {
											//
											e.printStackTrace();
										}
										onboard.setVisibility(View.VISIBLE);
										turncards(Player.myhand.get(2));
										myhandcard3
												.setVisibility(View.INVISIBLE);

										if (Player.myhand.get(2).cardType
												.equals("Character")) { // make
																		// player
																		// submit
																		// power
											Player.currentcardpower = Player.myhand
													.get(2).getPower();
											Player.currentcard = Player.myhand
													.get(2);
											Intent intent = new Intent(
													GamePlay.this,
													FractionInput.class);
											Globals.focusedActivity = "FractionInput";
											startActivity(intent);
											new WaitForInput()
													.execute(getApplicationContext());

										}
										updatelevels();
										if (Player.myhand.get(2).cardType
												.equals("Resource"))
											updatestrength();
										CurrentCard card = new CurrentCard(0);
										Player.myhand.set(2, card);
										break;
									// The 'which' argument contains the index
									// position
									// of the selected item
									case 1:
										new AlertDialog.Builder(GamePlay.this)
												.setTitle("Really Discard?")
												.setMessage(
														"Are you sure you want to discard this card")
												.setNegativeButton(
														android.R.string.no,
														null)
												.setPositiveButton(
														android.R.string.yes,
														new DialogInterface.OnClickListener() {

															public void onClick(
																	DialogInterface arg0,
																	int arg1) {
																if (Player.candiscard) {
																	ImageView image = (ImageView) findViewById(R.id.myhandcard3);
																	image.setVisibility(View.INVISIBLE);
																	CurrentCard nocard = new CurrentCard(
																			0);
																	Player.myhand
																			.set(2,
																					nocard);

																	Player.candiscard = false;

																} else {
																	AlertDialog.Builder builder = new AlertDialog.Builder(
																			GamePlay.this);
																	builder.setMessage(
																			"You have already discarded 1 card")
																			.setNegativeButton(
																					"Ok",
																					new DialogInterface.OnClickListener() {
																						@Override
																						public void onClick(
																								DialogInterface dialog,
																								int id) {

																							// User
																							// cancelled
																							// the
																							// dialog
																						}
																					});
																	// Create
																	// the
																	// AlertDialog
																	// object
																	// and
																	// return it
																	builder.create();
																	builder.show();
																}
															}
														}).create().show();

										break;
									}
								}

							});

					AlertDialog alertDialog = builder.create();
					alertDialog.show();

					WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

					lp.copyFrom(alertDialog.getWindow().getAttributes());
					lp.width = 250;
					lp.height = 500;
					lp.x = 170;
					lp.y = 100;
					alertDialog.getWindow().setAttributes(lp);

				}
			});
			myhandcard4.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					CharSequence[] array = { "Place Card On Field", "Discard",
							"X" };

					// img.set
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());

					// img2.setImageResource(id);
					Drawable drawable = zoomcard.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.myhand.get(3),
								Player.myhand.get(3).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}

					zoomcard.setVisibility(View.VISIBLE);
					// ContextThemeWrapper cw = new ContextThemeWrapper(
					// GamePlay.this, R.style.AlertDialogTheme );

					AlertDialog.Builder builder = new AlertDialog.Builder(
							GamePlay.this);

					SpannableStringBuilder ssBuilser = new SpannableStringBuilder(
							"Choose An Action");
					// StyleSpan span = new StyleSpan(Typeface.ITALIC);
					ScaleXSpan span1 = new ScaleXSpan(15);
					ssBuilser.setSpan(new AbsoluteSizeSpan(20), 0,
							ssBuilser.length(),
							Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

					builder.setTitle(ssBuilser);

					ArrayAdapter<CharSequence> itemsAdapter = new ArrayAdapter<CharSequence>(
							GamePlay.this, R.layout.menu_items, array);

					builder.setAdapter(itemsAdapter,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									switch (which) {

									case 0:
										if (!Player.canplayresource
												&& Player.myhand.get(3).cardType
														.equals("Resource")) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already placed one resource down")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {
																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										int find = Game
												.findposition(Player.myhand
														.get(3));
										if (find == -1) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Not enough Resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {
																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -2) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already have 3 resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -3) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Max cards on board")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										ImageView onboard = (ImageView) findViewById(find);
										int id = getResources().getIdentifier(
												"sample_0", "drawable",
												getPackageName());

										try {
											recyclebitmap(onboard);
											onboard.setImageDrawable(writeOnDrawable(
													id, Player.myhand.get(3),
													Player.myhand.get(3).cardID));
										} catch (IOException e) {
											e.printStackTrace();
										}
										onboard.setVisibility(View.VISIBLE);
										turncards(Player.myhand.get(3));
										myhandcard4
												.setVisibility(View.INVISIBLE);

										if (Player.myhand.get(3).cardType
												.equals("Character")) { // make
																		// player
																		// submit
																		// power
											Player.currentcardpower = Player.myhand
													.get(3).getPower();
											Player.currentcard = Player.myhand
													.get(3);
											Intent intent = new Intent(
													GamePlay.this,
													FractionInput.class);
											Globals.focusedActivity = "FractionInput";
											startActivity(intent);
											new WaitForInput()
													.execute(getApplicationContext());

										}
										updatelevels();
										if (Player.myhand.get(3).cardType
												.equals("Resource"))
											updatestrength();
										CurrentCard card = new CurrentCard(0);
										Player.myhand.set(3, card);
										break;
									// The 'which' argument contains the index
									// position
									// of the selected item
									case 1:
										new AlertDialog.Builder(GamePlay.this)
												.setTitle("Really Discard?")
												.setMessage(
														"Are you sure you want to discard this card")
												.setNegativeButton(
														android.R.string.no,
														null)
												.setPositiveButton(
														android.R.string.yes,
														new DialogInterface.OnClickListener() {

															public void onClick(
																	DialogInterface arg0,
																	int arg1) {
																if (Player.candiscard) {
																	ImageView image = (ImageView) findViewById(R.id.myhandcard4);
																	image.setVisibility(View.INVISIBLE);
																	CurrentCard nocard = new CurrentCard(
																			0);
																	Player.myhand
																			.set(3,
																					nocard);

																	Player.candiscard = false;

																} else {
																	AlertDialog.Builder builder = new AlertDialog.Builder(
																			GamePlay.this);
																	builder.setMessage(
																			"You have already discarded 1 card")
																			.setNegativeButton(
																					"Ok",
																					new DialogInterface.OnClickListener() {
																						@Override
																						public void onClick(
																								DialogInterface dialog,
																								int id) {

																							// User
																							// cancelled
																							// the
																							// dialog
																						}
																					});
																	// Create
																	// the
																	// AlertDialog
																	// object
																	// and
																	// return it
																	builder.create();
																	builder.show();
																}
															}
														}).create().show();

										break;
									}
								}

							});

					AlertDialog alertDialog = builder.create();
					alertDialog.show();

					WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

					lp.copyFrom(alertDialog.getWindow().getAttributes());
					lp.width = 250;
					lp.height = 500;
					lp.x = 170;
					lp.y = 100;
					alertDialog.getWindow().setAttributes(lp);

				}
			});
			myhandcard5.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					CharSequence[] array = { "Place Card On Field", "Discard",
							"X" };

					// img.set
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());

					// img2.setImageResource(id);
					Drawable drawable = zoomcard.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.myhand.get(4),
								Player.myhand.get(4).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}

					zoomcard.setVisibility(View.VISIBLE);
					// ContextThemeWrapper cw = new ContextThemeWrapper(
					// GamePlay.this, R.style.AlertDialogTheme );

					AlertDialog.Builder builder = new AlertDialog.Builder(
							GamePlay.this);

					SpannableStringBuilder ssBuilser = new SpannableStringBuilder(
							"Choose An Action");
					// StyleSpan span = new StyleSpan(Typeface.ITALIC);
					ScaleXSpan span1 = new ScaleXSpan(15);
					ssBuilser.setSpan(new AbsoluteSizeSpan(20), 0,
							ssBuilser.length(),
							Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

					builder.setTitle(ssBuilser);

					ArrayAdapter<CharSequence> itemsAdapter = new ArrayAdapter<CharSequence>(
							GamePlay.this, R.layout.menu_items, array);

					builder.setAdapter(itemsAdapter,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									switch (which) {

									case 0:
										if (!Player.canplayresource
												&& Player.myhand.get(4).cardType
														.equals("Resource")) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already placed one resource down")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {
																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										int find = Game
												.findposition(Player.myhand
														.get(4));
										if (find == -1) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Not enough Resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {
																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -2) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already have 3 resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -3) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Max cards on board")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										ImageView onboard = (ImageView) findViewById(find);
										int id = getResources().getIdentifier(
												"sample_0", "drawable",
												getPackageName());

										try {
											recyclebitmap(onboard);
											onboard.setImageDrawable(writeOnDrawable(
													id, Player.myhand.get(4),
													Player.myhand.get(4).cardID));
										} catch (IOException e) {
											e.printStackTrace();
										}
										onboard.setVisibility(View.VISIBLE);
										turncards(Player.myhand.get(4));
										myhandcard5
												.setVisibility(View.INVISIBLE);

										if (Player.myhand.get(4).cardType
												.equals("Character")) { // make
																		// player
																		// submit
																		// power
											Player.currentcardpower = Player.myhand
													.get(4).getPower();
											Player.currentcard = Player.myhand
													.get(4);
											Intent intent = new Intent(
													GamePlay.this,
													FractionInput.class);
											Globals.focusedActivity = "FractionInput";
											startActivity(intent);
											new WaitForInput()
													.execute(getApplicationContext());

										}
										updatelevels();
										if (Player.myhand.get(4).cardType
												.equals("Resource"))
											updatestrength();
										CurrentCard card = new CurrentCard(0);
										Player.myhand.set(4, card);
										break;
									// The 'which' argument contains the index
									// position
									// of the selected item
									case 1:
										new AlertDialog.Builder(GamePlay.this)
												.setTitle("Really Discard?")
												.setMessage(
														"Are you sure you want to discard this card")
												.setNegativeButton(
														android.R.string.no,
														null)
												.setPositiveButton(
														android.R.string.yes,
														new DialogInterface.OnClickListener() {

															public void onClick(
																	DialogInterface arg0,
																	int arg1) {
																if (Player.candiscard) {
																	ImageView image = (ImageView) findViewById(R.id.myhandcard5);
																	image.setVisibility(View.INVISIBLE);
																	CurrentCard nocard = new CurrentCard(
																			0);
																	Player.myhand
																			.set(4,
																					nocard);

																	Player.candiscard = false;

																} else {
																	AlertDialog.Builder builder = new AlertDialog.Builder(
																			GamePlay.this);
																	builder.setMessage(
																			"You have already discarded 1 card")
																			.setNegativeButton(
																					"Ok",
																					new DialogInterface.OnClickListener() {
																						@Override
																						public void onClick(
																								DialogInterface dialog,
																								int id) {

																							// User
																							// cancelled
																							// the
																							// dialog
																						}
																					});
																	// Create
																	// the
																	// AlertDialog
																	// object
																	// and
																	// return it
																	builder.create();
																	builder.show();
																}
															}
														}).create().show();

										break;

									}
								}

							});

					AlertDialog alertDialog = builder.create();
					alertDialog.show();

					WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

					lp.copyFrom(alertDialog.getWindow().getAttributes());
					lp.width = 250;
					lp.height = 500;
					lp.x = 170;
					lp.y = 100;
					alertDialog.getWindow().setAttributes(lp);

				}
			});
			myhandcard6.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					CharSequence[] array = { "Place Card On Field", "Discard",
							"X" };

					// img.set
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());

					// img2.setImageResource(id);
					Drawable drawable = zoomcard.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;

					}
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.myhand.get(5),
								Player.myhand.get(5).cardID));
					} catch (IOException e) {
						e.printStackTrace();
					}

					zoomcard.setVisibility(View.VISIBLE);
					// ContextThemeWrapper cw = new ContextThemeWrapper(
					// GamePlay.this, R.style.AlertDialogTheme );

					AlertDialog.Builder builder = new AlertDialog.Builder(
							GamePlay.this);

					SpannableStringBuilder ssBuilser = new SpannableStringBuilder(
							"Choose An Action");
					// StyleSpan span = new StyleSpan(Typeface.ITALIC);
					ScaleXSpan span1 = new ScaleXSpan(15);
					ssBuilser.setSpan(new AbsoluteSizeSpan(20), 0,
							ssBuilser.length(),
							Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

					builder.setTitle(ssBuilser);

					ArrayAdapter<CharSequence> itemsAdapter = new ArrayAdapter<CharSequence>(
							GamePlay.this, R.layout.menu_items, array);

					builder.setAdapter(itemsAdapter,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									switch (which) {

									case 0:
										if (!Player.canplayresource
												&& Player.myhand.get(5).cardType
														.equals("Resource")) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already placed one resource down")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {
																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}

										int find = Game
												.findposition(Player.myhand
														.get(5));
										if (find == -1) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Not enough Resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {
																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -2) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Already have 3 resources")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										if (find == -3) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"Max cards on board")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
											break;
										}
										ImageView onboard = (ImageView) findViewById(find);
										int id = getResources().getIdentifier(
												"sample_0", "drawable",
												getPackageName());

										try {
											recyclebitmap(onboard);
											onboard.setImageDrawable(writeOnDrawable(
													id, Player.myhand.get(5),
													Player.myhand.get(5).cardID));
										} catch (IOException e) {
											//
											e.printStackTrace();
										}
										onboard.setVisibility(View.VISIBLE);
										turncards(Player.myhand.get(5));
										myhandcard6
												.setVisibility(View.INVISIBLE);
										if (Player.myhand.get(5).cardType
												.equals("Character")) { // make
																		// player
																		// submit
																		// power
											Player.currentcardpower = Player.myhand
													.get(5).getPower();
											Player.currentcard = Player.myhand
													.get(5);
											Intent intent = new Intent(
													GamePlay.this,
													FractionInput.class);
											Globals.focusedActivity = "FractionInput";
											startActivity(intent);
										new WaitForInput()
													.execute(getApplicationContext());
										}

										updatelevels();
										if (Player.myhand.get(5).cardType
												.equals("Resource"))
											updatestrength();
										CurrentCard card = new CurrentCard(0);
										Player.myhand.set(5, card);
										break;
									// The 'which' argument contains the index
									// position
									// of the selected item
									case 1:
										new AlertDialog.Builder(GamePlay.this)
												.setTitle("Really Discard?")
												.setMessage(
														"Are you sure you want to discard this card")
												.setNegativeButton(
														android.R.string.no,
														null)
												.setPositiveButton(
														android.R.string.yes,
														new DialogInterface.OnClickListener() {

															public void onClick(
																	DialogInterface arg0,
																	int arg1) {
																if (Player.candiscard) {
																	ImageView image = (ImageView) findViewById(R.id.myhandcard6);
																	image.setVisibility(View.INVISIBLE);
																	CurrentCard nocard = new CurrentCard(
																			0);
																	Player.myhand
																			.set(5,
																					nocard);

																	Player.candiscard = false;

																} else {
																	AlertDialog.Builder builder = new AlertDialog.Builder(
																			GamePlay.this);
																	builder.setMessage(
																			"You have already discarded 1 card")
																			.setNegativeButton(
																					"Ok",
																					new DialogInterface.OnClickListener() {
																						@Override
																						public void onClick(
																								DialogInterface dialog,
																								int id) {

																							// User
																							// cancelled
																							// the
																							// dialog
																						}
																					});
																	// Create
																	// the
																	// AlertDialog
																	// object
																	// and
																	// return it
																	builder.create();
																	builder.show();
																}
															}
														}).create().show();

										break;
									}
								}

							});

					AlertDialog alertDialog = builder.create();
					alertDialog.show();

					WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

					lp.copyFrom(alertDialog.getWindow().getAttributes());
					lp.width = 250;
					lp.height = 500;
					lp.x = 170;
					lp.y = 100;
					alertDialog.getWindow().setAttributes(lp);

				}
			});

			zoomcard.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());

					// img2.setImageResource(id);
					// img2.setImageDrawable(writeOnDrawable(id,"test"));
					// zoomcard.setImageDrawable(writeOnDrawable(id,"test",1));
					zoomcard.setVisibility(View.INVISIBLE);
					Drawable drawable = zoomcard.getDrawable();
					if (drawable instanceof BitmapDrawable) {
						BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
						if (null != bitmapDrawable
								&& !bitmapDrawable.getBitmap().isRecycled()) {

						} else {

							Log.e("recycled", "bitmap is already recycled");
						}
						bitmapDrawable = null;
					}
				}
			});
			new GetMyCards().execute(getApplicationContext());

		} catch (Exception e) {
			// this is the line of code that sends a real error message to the
			// log
			Log.e("ERROR", "ERROR IN CODE: " + e.toString());

			// this is the line that prints out the location in
			// the code where the error occurred.
			e.printStackTrace();
		}

	}
	public class EndTurn extends AsyncTask<Context, Void, Context> {
		@Override
		protected Context doInBackground(Context... Contexts) {
			final RestClient Connect = new RestClient(GamePlay.this);


			Player.endturn = true;
			String p = Player.getPlayer();

			String sendhealth = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
					+ Game.getGameID()
					+ "&"
					+ Player.getPlayer()
					+ "power=" + Game.getmyhealthstring();
			Connect.connect(sendhealth);
			Connect.clear();

			String sendstrength = "http://emath-project.appspot.com/emath/emathservicehttp?function=setaction&gameID="
					+ Game.getGameID()
					+ "&"
					+ "round=challenge"
					+ "&"
					+ "turn="
					+ Player.getPlayer()
					+ "&"
					+ "action="
					+ Game.getmypowerlevelstring();
			Connect.connect(sendstrength);
			Connect.clear();

			Game.firstturn = false;
			Game.setmystacks();
			Player.waschallenged = false;
			String turn = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
					+ Game.getGameID()
					+ "&turn="
					+ OpponentPlayer.getPlayer()
					+ "&"
					+ Player.getPlayer()
					+ "power="
					+ Game.getmyhealthstring()
					+ "&"
					+ p
					+ "strength="
					+ Game.toFractionURL(Game.getmypowerlevel(), 1000)
					+ ","
					+ String.valueOf(Game.mydiceroll)
					+ ","
					+ String.valueOf(Game.opponentdiceroll);
			Connect.connect(turn);
			Connect.clear();
			Player.donewaiting = false;
			
			return Contexts[0];
		}

		@Override
		protected void onPreExecute() {
			
			if (dialog != null)
				dialog.dismiss();
			dialog2 = new ProgressDialog(GamePlay.this);
			dialog2.setMessage("Waiting for opponent to move");
			dialog2.setCancelable(false);
			dialog2.setProgressStyle(android.R.attr.progressBarStyleSmall);
			dialog2.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							task.cancel(true);
							cancelandexit();

						}
					});
			dialog2.show();

		}

		@Override
		protected void onPostExecute(Context result) {
			
			task = new WaitTurn();
			task.execute(getApplicationContext());
			
			
		}

	}
	public class UpdateGame extends AsyncTask<Context, Void, Context> {
		@Override
		protected Context doInBackground(Context... Contexts) {

			while (!Player.endturn && !task2.isCancelled()) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					//
					e.printStackTrace();
				}
				Game.setmystacks();
			}
			Player.endturn = false; 
			return Contexts[0];
		}

		@Override
		protected void onPreExecute() {

		}

		@Override
		protected void onPostExecute(Context result) {

		}

	}

	public class UpdateBoard extends AsyncTask<Context, Void, Context> {
		@Override
		protected Context doInBackground(Context... Contexts) {

			// SystemClock.sleep(500);
			try {
				Game.updateboard2();
			} catch (JSONException e) {
				//
				e.printStackTrace();
			}
			return Contexts[0];
		}

		@Override
		protected void onPreExecute() {

		}

		@Override
		protected void onPostExecute(Context result) {
			/*
			 * try {
			 * 
			 * updateboard(); } catch (JSONException e) { catch block
			 * e.printStackTrace(); }
			 */
			/*
			 * RestClient Connect = new RestClient();
			 * 
			 * String p = OpponentPlayer.player; List<String>
			 * strengthanddiceroll = null;
			 * 
			 * String strength = ""; String getstrength =
			 * "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
			 * + Game.GameID + "&" + p + "strength";
			 * Connect.connect(getstrength); int checkconnect =
			 * Connect.findindex(p + "strength"); if (checkconnect != -1) { try
			 * { strengthanddiceroll = Game.parsecards(Connect.valArray
			 * .getString(checkconnect)); } catch (JSONException e) {
			 * Auto-generated catch block e.printStackTrace(); } strength =
			 * strengthanddiceroll.get(0); Game.mydiceroll =
			 * Integer.parseInt(strengthanddiceroll.get(1));
			 * Game.opponentdiceroll = Integer
			 * .parseInt(strengthanddiceroll.get(2));
			 * 
			 * TextView power = (TextView) findViewById(R.id.theirpower); String
			 * powerlv = "Foe Strength: "; power.setText(powerlv + strength); }
			 * Connect.clear();
			 */
			// Game.updateboard();
			// discardanduntap();
			clearopponentstacks();
			updatestrength();

			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			for (int i = 0; i < OpponentPlayer.stack1.size(); i++) {

				if (OpponentPlayer.stack1.get(i).cardID != 0) {
					switch (i) {

					case 0:

						ImageView img = (ImageView) findViewById(R.id.stack1card1);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack1.get(i),
								OpponentPlayer.stack1.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack1.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}

						break;

					case 1:

						img = (ImageView) findViewById(R.id.stack1card2);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack1.get(i),
								OpponentPlayer.stack1.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack1.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					case 2:

						img = (ImageView) findViewById(R.id.stack1card3);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack1.get(i),
								OpponentPlayer.stack1.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack1.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					}
				}
			}

			for (int i = 0; i < OpponentPlayer.stack2.size(); i++) {
				if (OpponentPlayer.stack2.get(i).cardID != 0) {
					switch (i) {

					case 0:

						ImageView img = (ImageView) findViewById(R.id.stack2card1);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack2.get(i),
								OpponentPlayer.stack2.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack2.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					case 1:

						img = (ImageView) findViewById(R.id.stack2card2);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack2.get(i),
								OpponentPlayer.stack2.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack2.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					case 2:

						img = (ImageView) findViewById(R.id.stack2card3);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack2.get(i),
								OpponentPlayer.stack2.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack2.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					}

				}
			}
			for (int i = 0; i < OpponentPlayer.stack3.size(); i++) {
				if (OpponentPlayer.stack3.get(i).cardID != 0) {
					switch (i) {

					case 0:

						ImageView img = (ImageView) findViewById(R.id.stack3card1);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack3.get(i),
								OpponentPlayer.stack3.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack3.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					case 1:

						img = (ImageView) findViewById(R.id.stack3card2);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack3.get(i),
								OpponentPlayer.stack3.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack3.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					case 2:

						img = (ImageView) findViewById(R.id.stack3card3);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack3.get(i),
								OpponentPlayer.stack3.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack3.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					}

				}
			}

			for (int i = 0; i < OpponentPlayer.stack4.size(); i++) {
				if (OpponentPlayer.stack4.get(i).cardID != 0) {
					switch (i) {

					case 0:

						ImageView img = (ImageView) findViewById(R.id.stack4card1);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack4.get(i),
								OpponentPlayer.stack4.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack4.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					case 1:

						img = (ImageView) findViewById(R.id.stack4card2);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack4.get(i),
								OpponentPlayer.stack4.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack4.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					case 2:

						img = (ImageView) findViewById(R.id.stack4card3);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack4.get(i),
								OpponentPlayer.stack4.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack4.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					}

				}
			}
			for (int i = 0; i < OpponentPlayer.stack5.size(); i++) {
				if (OpponentPlayer.stack5.get(i).cardID != 0) {
					switch (i) {

					case 0:

						ImageView img = (ImageView) findViewById(R.id.stack5card1);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack5.get(i),
								OpponentPlayer.stack5.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack5.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					case 1:

						img = (ImageView) findViewById(R.id.stack5card2);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack5.get(i),
								OpponentPlayer.stack5.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack5.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					case 2:

						img = (ImageView) findViewById(R.id.stack5card3);
						recyclebitmap(img);
						img.setImageDrawable(writeOnDrawableReverse(id,
								OpponentPlayer.stack5.get(i),
								OpponentPlayer.stack5.get(i).cardID));
						img.setVisibility(View.VISIBLE);
						if (OpponentPlayer.stack5.get(i).tapped == true) {
							RotateAnimation anim2 = new RotateAnimation(90, 90,
									Animation.RELATIVE_TO_SELF, 0.5f,
									Animation.RELATIVE_TO_SELF, 0.5f);

							anim2.setFillAfter(true);
							anim2.setDuration(0);
							img.startAnimation(anim2);
						}
						break;

					}

				}
			}

			if (!task.isCancelled() && !Player.donewaiting)
				mHandler.postDelayed(updateboardloop, 100);
		}

	}

	public class WaitTurn extends AsyncTask<Context, Void, Context> {
		int flag = 0;

		RestClient Connect = new RestClient(GamePlay.this);
		@Override
		protected Context doInBackground(Context... Contexts) {

			String wait = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
					+ Game.getGameID()
					+ "&round&turn&"
					+ OpponentPlayer.getPlayer() + "power";
			int checkconnect = -1;
			int checkconnect2 = -1;
			int checkconnect3 = -1;
			int checkconnect4 = -1;
			int checkconnectpower = -1;
			String otherplayer;
			if (Player.getPlayer().equals("p1"))
				otherplayer = "player2";
			else
				otherplayer = "player1";
			while (checkconnect == -1) {
				/*
				 * try { //updateboard(); } catch (JSONException e) { // TODO
				 * Auto-generated catch block e.printStackTrace(); }
				 */
				if (task.isCancelled()) {

					return Contexts[0];

				}
				Connect.connect(wait);
				checkconnect = Connect.findindex("turn");
				checkconnect2 = Connect.findindex("round");
				checkconnect3 = Connect.findindex(otherplayer);
				checkconnectpower = Connect.findindex(OpponentPlayer
						.getPlayer() + "power");
				if (checkconnectpower != -1) {
					try {
						if (Connect.getvalArray().getString(checkconnectpower)
								.equals("1000")) {
							// quitgame();
							flag = 6;
							return Contexts[0];
						}
					} catch (JSONException e) {
						//
						e.printStackTrace();
					}
				}
				Connect.clear();

			}

			while (true) {
				if (task.isCancelled())
					return Contexts[0];
				// publishProgress();
				// SystemClock.sleep(100);
				Connect.connect(wait);
				checkconnect = Connect.findindex("turn");
				checkconnectpower = Connect.findindex(OpponentPlayer
						.getPlayer() + "power");

				try {
					if (checkconnect3 == -1) {
						publishProgress();

						task.cancel(true);
						if (task.isCancelled()) {

							return Contexts[0];

						}

					}
					if (checkconnectpower != -1)
						if (Connect.getvalArray().getString(checkconnectpower)
								.equals("1000")) {
							// quitgame();
							flag = 6;
							return Contexts[0];
						}

					if (Connect.getvalArray().getString(checkconnect)
							.equals(Player.getPlayer())) {
						Player.donewaiting = true;
						if (Connect.getvalArray().getString(checkconnect2)
								.contains("ChallengeWon")) {
							flag = 1;
							// break;

						}
						if (Connect.getvalArray().getString(checkconnect2)
								.contains("finished")) {
							publishProgress();
						}
						if (Connect.getvalArray().getString(checkconnect2)
								.contains("ChallengeLost")) {
							flag = 2;

						}
						if (Connect.getvalArray().getString(checkconnect2)
								.contains("ChallengeTie")) {
							flag = 3;
							// break;

						}
						if (Connect.getvalArray().getString(checkconnect2)
								.contains("Outofcardswon")) {
							flag = 4;
							// break;

						}
						if (Connect.getvalArray().getString(checkconnect2)
								.contains("Outofcardslost")) {
							flag = 5;
							// break;

						}
						Connect.clear();
						break;
					}
				} catch (JSONException e) {
					//
					e.printStackTrace();
				}
				Connect.clear();
			}

			Connect.clear();

			return Contexts[0];
		}

		protected void getopponenthealth() {
			String gethealth = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
					+ Game.getGameID()
					+ "&"
					+ OpponentPlayer.getPlayer()
					+ "power"; // health
			Connect.connect(gethealth);
			int checkconnect = Connect.findindex(OpponentPlayer.getPlayer()
					+ "power");
			try {
				String fraction = Connect.getvalArray().getString(checkconnect);
				List<String> fractions = Arrays.asList(fraction
						.split("\\s*/\\s*"));

				if (fractions.size() == 1)
					OpponentPlayer.health = BigRational.valueOf(fractions
							.get(0));
				if (fractions.size() == 2)
					OpponentPlayer.health = BigRational.getInstance(
							fractions.get(0), fractions.get(1));
				// OpponentPlayer.health=BigRational.valueOf(Connect.getvalArray().getString(checkconnect));
				TextView theirhealth = (TextView) findViewById(R.id.myhealth);
				String healthlv = "Foe Points: ";
				theirhealth.setText(healthlv
						+ Game.toFraction(OpponentPlayer.health.doubleValue(),
								1000));
			} catch (JSONException e) {
				//
				e.printStackTrace();
			}
			Connect.clear();
		}

		@Override
		protected void onProgressUpdate(Void... v) {
			String message = "You have won,the other player has forfeited";
			AlertDialog.Builder builder = new AlertDialog.Builder(GamePlay.this);
			builder.setMessage(message).setNegativeButton("Ok",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int id) {

							String endgame = "http://emath-project.appspot.com/emath/emathservicehttp?function=endgame&gameID="
									+ Game.getGameID();
							Connect.connect(endgame);
							Connect.clear();

							String gettoken = "http://emath-project.appspot.com/emath/emathservicehttp?function=getuser&userID="
									+ Username.getUser();
							Connect.connect(gettoken);
							int checkconnect = Connect.findindex("tokens");
							String settoken = null;
							try {
								int token = Integer.parseInt(Connect.valArray
										.getString(checkconnect));
								token = token + 1;
								checkconnect = Connect.findindex("skillLevel");
								int skillevel = Integer
										.parseInt(Connect.valArray
												.getString(checkconnect));
								skillevel = skillevel + 1;
								settoken = "http://emath-project.appspot.com/emath/emathservicehttp?function=setuser&userID="
										+ Username.getUser()
										+ "&tokens="
										+ String.valueOf(token)
										+ "&skillLevel="
										+ String.valueOf(skillevel);
								Connect.connect(settoken);
							} catch (JSONException e1) {
								e1.printStackTrace();
							}

							try {
								dialog2.dismiss();
								dialog2 = null;
							} catch (Exception e) {
							}

							Connect.clear();
							if (task2 != null && !task2.isCancelled())
								task2.cancel(true);

							finish();
							// User cancelled the dialog
						}
					});
			builder.create();
			builder.show();

			// Game.setmystacks();
			/*
			 * try { //updateboard(); } catch (JSONException e) { Auto-generated
			 * catch block e.printStackTrace(); }
			 */
		}

		@Override
		protected void onPreExecute() {
			if (dialog != null)
				dialog.dismiss();
			if(dialog2 == null||!dialog2.isShowing()){
			dialog2 = new ProgressDialog(GamePlay.this);
			dialog2.setMessage("Waiting for opponent to move");
			dialog2.setCancelable(false);
			dialog2.setProgressStyle(android.R.attr.progressBarStyleSmall);
			dialog2.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							task.cancel(true);
							cancelandexit();

						}
					});
			dialog2.show();
			}

		}

		@Override
		protected void onPostExecute(Context result) {
			
			Button endturn = (Button) findViewById(R.id.endturn);
			Button challenge = (Button) findViewById(R.id.challenge);

			endturn.setEnabled(true);
			challenge.setEnabled(true);

			if (task.isCancelled())
				return;

			try {
				if (!Game.firstturn) {
					challenge.setVisibility(View.VISIBLE);
				}
				Player.canplayresource = true;
				Player.candiscard = true;

				updateboard();
			//	task2 = new UpdateGame();
			//	task2.execute(getApplicationContext());

				if (flag == 0) {
					unturncards();
					updatestrength();
					getopponenthealth();

					// new UpdateGame().execute(getApplicationContext());
				}
				if (flag == 1) {
					String message = "You have been challenged!";
					AlertDialog.Builder builder = new AlertDialog.Builder(
							GamePlay.this);
					builder.setMessage(message).setNegativeButton("Ok",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int id) {

									try {
										Player.challenger = false;
										challenge(false);
										Player.waschallenged = true;
										// endgame(false);
										unturncards();
										updatestrength();
										getopponenthealth();
										String clearchallenge = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
												+ Game.getGameID()
												+ "&round=ingame";
										Connect.connect(clearchallenge);
										Connect.clear();
										flag = 0;
										// new
										// UpdateGame().execute(getApplicationContext());

									} catch (JSONException e) {
										e.printStackTrace();
									}

									// User cancelled the dialog
								}
							});
					builder.create();
					builder.show();

				}

				if (flag == 2) {
					int d1 = 0;
					int d2 = 0;
					List<String> strengthanddiceroll;
					String getstrength = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
							+ Game.getGameID()
							+ "&"
							+ OpponentPlayer.getPlayer() + "strength";
					Connect.connect(getstrength);
					int checkconnect = Connect.findindex(OpponentPlayer
							.getPlayer() + "strength");
					if (checkconnect != -1) {
						strengthanddiceroll = Game.parsecards(Connect.valArray
								.getString(checkconnect));

						d1 = Integer.parseInt(strengthanddiceroll.get(1));
						d2 = Integer.parseInt(strengthanddiceroll.get(2));
					}

					MediaPlayer mp = MediaPlayer.create(GamePlay.this,
							R.raw.diceroll);
					mp.start();
					Vibrator vibrator = (Vibrator) getSystemService(Activity.VIBRATOR_SERVICE);
					vibrator.vibrate(Globals.vibrateTime);

					Context context = getApplicationContext(); // toast for dice
																// roll
					CharSequence text = "Your dice rolled ";
					// CharSequence text2 = "Opponent's dice rolled ";
					int duration = Toast.LENGTH_LONG;
					text = text + String.valueOf(d1) + ". ";
					text = text + "Opponent's dice rolled "
							+ String.valueOf(d2) + ".";
					Toast toast = Toast.makeText(context, text, duration);
					toast.setGravity(Gravity.BOTTOM, 230, 400);
					toast.show();

					String message = "You have been challenged and won!";
					AlertDialog.Builder builder = new AlertDialog.Builder(
							GamePlay.this);
					builder.setMessage(message).setNegativeButton("Ok",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int id) {
									getopponenthealth();

									endgame(false);
									String clearchallenge = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
											+ Game.getGameID()
											+ "&round=ingame";
									Connect.connect(clearchallenge);
									Connect.clear();
									flag = 0;
									unturncards();
									updatestrength();
									// new
									// UpdateGame().execute(getApplicationContext());
									// User cancelled the dialog
								}
							});

					builder.create();
					builder.show();

				}
				if (flag == 3) {
					int d1 = 0;
					int d2 = 0;
					List<String> strengthanddiceroll;
					String getstrength = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
							+ Game.getGameID()
							+ "&"
							+ OpponentPlayer.getPlayer() + "strength";
					Connect.connect(getstrength);
					int checkconnect = Connect.findindex(OpponentPlayer
							.getPlayer() + "strength");
					if (checkconnect != -1) {
						strengthanddiceroll = Game.parsecards(Connect.valArray
								.getString(checkconnect));

						d1 = Integer.parseInt(strengthanddiceroll.get(1));
						d2 = Integer.parseInt(strengthanddiceroll.get(2));
					}

					MediaPlayer mp = MediaPlayer.create(GamePlay.this,
							R.raw.diceroll);
					mp.start();
					Vibrator vibrator = (Vibrator) getSystemService(Activity.VIBRATOR_SERVICE);
					vibrator.vibrate(Globals.vibrateTime);

					Context context = getApplicationContext(); // toast for dice
																// roll
					CharSequence text = "Your dice rolled ";
					// CharSequence text2 = "Opponent's rolled a ";
					int duration = Toast.LENGTH_LONG;
					text = text + String.valueOf(d1) + ". ";
					text = text + "Opponent's dice rolled "
							+ String.valueOf(d2) + ".";
					Toast toast = Toast.makeText(context, text, duration);
					toast.setGravity(Gravity.BOTTOM, 230, 400);

					toast.show();

					String message = "You have been challenged and tied!";
					AlertDialog.Builder builder = new AlertDialog.Builder(
							GamePlay.this);
					builder.setMessage(message).setNegativeButton("Ok",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int id) {
									getopponenthealth();
									String clearchallenge = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
											+ Game.getGameID()
											+ "&round=ingame";
									Connect.connect(clearchallenge);
									Connect.clear();
									flag = 0;
									unturncards();
									updatestrength();
									// new
									// UpdateGame().execute(getApplicationContext());
									// User cancelled the dialog
								}
							});
					builder.create();
					builder.show();
				}
				if (flag == 4) {
					String message = "You have won,the other player has run out of cards and you have greater power!";
					AlertDialog.Builder builder = new AlertDialog.Builder(
							GamePlay.this);
					builder.setMessage(message).setNegativeButton("Ok",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int id) {

									String endgame = "http://emath-project.appspot.com/emath/emathservicehttp?function=endgame&gameID="
											+ Game.getGameID();
									Connect.connect(endgame);
									Connect.clear();

									String gettoken = "http://emath-project.appspot.com/emath/emathservicehttp?function=getuser&userID="
											+ Username.getUser();
									Connect.connect(gettoken);
									int checkconnect = Connect
											.findindex("tokens");
									String settoken = null;
									try {
										int token = Integer.parseInt(Connect.valArray
												.getString(checkconnect));
										token = token + 1;
										checkconnect = Connect
												.findindex("skillLevel");
										int skillevel = Integer.parseInt(Connect.valArray
												.getString(checkconnect));
										skillevel = skillevel + 1;
										settoken = "http://emath-project.appspot.com/emath/emathservicehttp?function=setuser&userID="
												+ Username.getUser()
												+ "&tokens="
												+ String.valueOf(token)
												+ "&skillLevel="
												+ String.valueOf(skillevel);
										Connect.connect(settoken);
									} catch (JSONException e1) {
										e1.printStackTrace();
									}

									try {
										dialog2.dismiss();
										dialog2 = null;
									} catch (Exception e) {
									}
									if (task2 != null && !task2.isCancelled())
										task2.cancel(true);
									Connect.clear();

									finish();

									// User cancelled the dialog
								}
							});
					builder.create();
					builder.show();
				}
				if (flag == 5) {
					String message = "You have lost,the other player has run out of cards and you have less power!";
					AlertDialog.Builder builder = new AlertDialog.Builder(
							GamePlay.this);
					builder.setMessage(message).setNegativeButton("Ok",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int id) {

									String endgame = "http://emath-project.appspot.com/emath/emathservicehttp?function=endgame&gameID="
											+ Game.getGameID();
									Connect.connect(endgame);
									Connect.clear();

									try {
										dialog2.dismiss();
										dialog2 = null;
									} catch (Exception e) {
									}

									Connect.clear();
									if (task2 != null && !task2.isCancelled())
										task2.cancel(true);
									finish();

									// User cancelled the dialog
								}
							});
					builder.create();
					builder.show();
				}
				if (flag == 6)
					quitgame();

			} catch (JSONException e) {
				//
				e.printStackTrace();
			}
			try {
				if (outofcards() != 1)
					drawcard();

				if (Player.mydeck.size() != 0)
					drawcard();

				if (Player.mydeck.size() != 0)
					drawcard();

				if (Player.mydeck.size() != 0)
					drawcard();

				if (Player.mydeck.size() != 0)
					drawcard();

				if (Player.mydeck.size() != 0)
					drawcard();

				TextView numcards = (TextView) findViewById(R.id.numcards);
				String number = "";
				numcards.setText(number + String.valueOf(Player.mydeck.size()));

			} catch (IOException e1) {
				//
				e1.printStackTrace();
			}
			try {
				dialog2.dismiss();
				dialog2 = null;
			} catch (Exception e) {
			}

		}
	}

	public class DownloadCards extends AsyncTask<Context, Void, Context> {
		@Override
		protected Context doInBackground(Context... Contexts) {
			/*
			 * try { downloadcardlibrary(); } catch (JSONException e) {
			 * Auto-generated catch block e.printStackTrace(); }
			 */
			while (true) {
				if (Counter.count == (Bitmaparray.CardurlArray.size()))
					break;
			}
			return Contexts[0];
		}

		@Override
		protected void onPreExecute() {

			// downloadcardlibrary();

		}

		@Override
		protected void onPostExecute(Context result) {
			try {
				initboard();
				// new UpdateGame().execute(getApplicationContext());
			} catch (JSONException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				dialog.dismiss();
				dialog = null;
			} catch (Exception e) {
			}

		}
	}

	public class GetMyCards extends AsyncTask<Context, Void, Context> {
		@Override
		protected Context doInBackground(Context... Contexts) {
			/*
			 * try { downloadcardlibrary(); } catch (JSONException e) {
			 * Auto-generated catch block e.printStackTrace(); }
			 */
			try {
				getcardlibrarydata();
			} catch (JSONException e) {
				//
				e.printStackTrace();
			}

			return Contexts[0];
		}

		@Override
		protected void onPreExecute() {

			dialog = ProgressDialog.show(GamePlay.this, "",
					"Downloading library", true);
		//	logHeap();
		}

		@Override
		protected void onPostExecute(Context result) {
			try {
				initboard();
				// new UpdateGame().execute(getApplicationContext());
			} catch (JSONException e1) {
				//
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				dialog.dismiss();
				dialog = null;
			} catch (Exception e) {
				// nothing
			}

			// new DownloadCards().execute(getApplicationContext());

		}
	}

	public class WaitForInput extends AsyncTask<Context, Void, Context> {
		@Override
		protected Context doInBackground(Context... Contexts) {

			while (!Player.finished) {

			
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				//
				e.printStackTrace();
			}
			}
			return Contexts[0];
		}

		@Override
		protected void onPreExecute() {
			Player.finished = false;
		}

		@Override
		protected void onPostExecute(Context result) {
			TextView power = (TextView) findViewById(R.id.mypower);
			String powerlv = "My Strength: ";
			power.setText(powerlv
					+ Game.toFraction(Game.getmypowerlevel(), 1000));

			if (Player.correct) {
				TextView myhealth = (TextView) findViewById(R.id.theirhealth); // I
																				// know
																				// this
																				// is
																				// reversed.
																				// It
																				// is
																				// a
																				// bug
				String myhealthlv = "My Points: ";
				// Player.correct = true;
				if (Globals.settingsHolder[3] == 0) // Easy Difficulty
				{
					Player.health = Player.health.add(BigRational.valueOf("1"));
				} else if (Globals.settingsHolder[3] == 1) // Medium Difficulty
				{
					Player.health = Player.health.add(BigRational.valueOf("2"));
				} else if (Globals.settingsHolder[3] == 2) // Hard Difficulty
				{
					Player.health = Player.health.add(BigRational.valueOf("3"));
				}

				myhealth.setText(myhealthlv
						+ Game.toFraction(Player.health.doubleValue(), 1000));
			} else // Incorrect
			{
				TextView myhealth = (TextView) findViewById(R.id.theirhealth); // I

				String myhealthlv = "My Points: ";
				// Player.correct = false;
				if (Globals.settingsHolder[3] == 0) // Easy Difficulty
				{
					// Do Nothing
					// Player.health =
					// Player.health.subtract(BigRational.valueOf("0"));
				} else if (Globals.settingsHolder[3] == 1) // Medium Difficulty
				{
					Player.health = Player.health.subtract(BigRational
							.valueOf("1"));
				} else if (Globals.settingsHolder[3] == 2) // Hard Difficulty
				{
					Player.health = Player.health.subtract(BigRational
							.valueOf("2"));
				}

				myhealth.setText(myhealthlv
						+ Game.toFraction(Player.health.doubleValue(), 1000));
			}

		}
	}

	@Override 
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.ingamemenu, menu);
		return true;
	}
	public void displayhelp() {

		HelpDialog helpDialog = new HelpDialog(GamePlay.this);
		helpDialog.setTitle("Help contents");
		helpDialog.show();
	}
	public void exittomenu() {

		final RestClient Connect = new RestClient(GamePlay.this);
		new AlertDialog.Builder(this)
				.setTitle("Really Exit?")
				.setMessage("Are you sure you want to forfeit and exit?")
				.setNegativeButton(android.R.string.no, null)
				.setPositiveButton(android.R.string.yes,
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface arg0, int arg1) {
								String finished = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
										+ Game.getGameID()
										+ "&"
										+ "round="
										+ "finished";
								Connect.connect(finished);
								Player.waschallenged = false;
								String turn = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
										+ Game.getGameID()
										+ "&turn="
										+ OpponentPlayer.getPlayer()
										+ "&"
										+ Player.getPlayer()
										+ "power="
										+ Game.getmyhealthstring()
										+ "&"
										+ Player.getPlayer()
										+ "strength="
										+ Game.toFractionURL(
												Game.getmypowerlevel(), 1000)
										+ ","
										+ String.valueOf(Game.mydiceroll)
										+ ","
										+ String.valueOf(Game.opponentdiceroll);
								Connect.connect(turn);
								Connect.clear();
								if (task2 != null && !task2.isCancelled())
									task2.cancel(true);
								if (task != null)
									task.cancel(true);

								finish();

							}
						}).create().show();
	}

	public void cancelandexit() {
		final RestClient Connect = new RestClient();

		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_POSITIVE:
					Player.waschallenged = false;
					String quitgame = "http://emath-project.appspot.com/emath/emathservicehttp?function=quitgame&gameID="
							+ Game.getGameID()
							+ "&"
							+ "userID="
							+ Username.getUser();
					Connect.connect(quitgame);

					Connect.clear();
					if (task2 != null && !task2.isCancelled())
						task2.cancel(true);
					try {
						dialog2.dismiss();
						dialog2 = null;
					} catch (Exception e) {
					}

					finish();
					break;

				case DialogInterface.BUTTON_NEGATIVE:
					Player.donewaiting = false;
					task = new WaitTurn();
					task.execute(getApplicationContext());
				//	loopupdateboard();

					break;
				}
			}
		};
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to forfeit and exit?")
				.setPositiveButton("Yes", dialogClickListener)
				.setNegativeButton("No", dialogClickListener).show();

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.exit:
			exittomenu();
			return true;
		case R.id.help:
			displayhelp();
			return true;
		}
		return super.onOptionsItemSelected(item);

	}

	public void onClickImage4(View View) {

	}

	public void onClickstack1card1(View View) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack1.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack1.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack1.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack1.get(index),
						OpponentPlayer.stack1.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 1;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack1.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack1.get(0),
								OpponentPlayer.stack1.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 1;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack1.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack1.get(1),
								OpponentPlayer.stack1.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
				if (OpponentPlayer.stack1.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack1.get(2),
								OpponentPlayer.stack1.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
			}
		}
	}

	public void onClickstack1card2(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack1.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack1.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack1.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack1.get(index),
						OpponentPlayer.stack1.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 1;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack1.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack1.get(0),
								OpponentPlayer.stack1.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 1;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack1.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack1.get(1),
								OpponentPlayer.stack1.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
				if (OpponentPlayer.stack1.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack1.get(2),
								OpponentPlayer.stack1.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
			}
		}
	}

	public void onClickstack1card3(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack1.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack1.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack1.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack1.get(index),
						OpponentPlayer.stack1.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 1;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack1.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack1.get(0),
								OpponentPlayer.stack1.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 1;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack1.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack1.get(1),
								OpponentPlayer.stack1.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
				if (OpponentPlayer.stack1.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack1.get(2),
								OpponentPlayer.stack1.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
			}
		}
	}

	public void onClickstack2card1(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack2.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack2.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack2.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack2.get(index),
						OpponentPlayer.stack2.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 2;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack2.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack2.get(0),
								OpponentPlayer.stack2.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 2;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack2.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack2.get(1),
								OpponentPlayer.stack2.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 2;
					break;
				}
				if (OpponentPlayer.stack2.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack2.get(2),
								OpponentPlayer.stack2.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 2;
					break;
				}
			}
		}
	}

	public void onClickstack2card2(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack2.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack2.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack2.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack2.get(index),
						OpponentPlayer.stack2.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 2;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack2.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack2.get(0),
								OpponentPlayer.stack2.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 2;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack2.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack2.get(1),
								OpponentPlayer.stack2.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
				if (OpponentPlayer.stack2.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack2.get(2),
								OpponentPlayer.stack2.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 2;
					break;
				}
			}
		}
	}

	public void onClickstack2card3(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack2.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack2.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack2.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack2.get(index),
						OpponentPlayer.stack2.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 2;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack2.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack2.get(0),
								OpponentPlayer.stack2.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 2;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack2.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack2.get(1),
								OpponentPlayer.stack2.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 2;
					break;
				}
				if (OpponentPlayer.stack2.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack2.get(2),
								OpponentPlayer.stack2.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 2;
					break;
				}
			}
		}
	}

	public void onClickstack3card1(View view) {
		Game.stack = 3;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack3.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack3.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack3.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack3.get(index),
						OpponentPlayer.stack3.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 3;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack3.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack3.get(0),
								OpponentPlayer.stack3.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 3;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack3.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack3.get(1),
								OpponentPlayer.stack3.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
				if (OpponentPlayer.stack3.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack3.get(2),
								OpponentPlayer.stack3.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
			}
		}
	}

	public void onClickstack3card2(View view) {
		Game.stack = 3;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack3.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack3.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack3.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack3.get(index),
						OpponentPlayer.stack3.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 3;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack3.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack3.get(0),
								OpponentPlayer.stack3.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 3;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack3.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack3.get(1),
								OpponentPlayer.stack3.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
				if (OpponentPlayer.stack3.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack3.get(2),
								OpponentPlayer.stack3.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
			}
		}
	}

	public void onClickstack3card3(View view) {
		Game.stack = 3;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack3.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack3.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack3.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack3.get(index),
						OpponentPlayer.stack3.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 3;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack3.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack3.get(0),
								OpponentPlayer.stack3.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 3;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack3.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack3.get(1),
								OpponentPlayer.stack3.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
				if (OpponentPlayer.stack3.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack3.get(2),
								OpponentPlayer.stack3.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
			}
		}

	}

	public void onClickstack4card1(View view) {
		Game.stack = 4;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack4.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack4.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack4.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack4.get(index),
						OpponentPlayer.stack4.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 4;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack4.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack4.get(0),
								OpponentPlayer.stack4.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 4;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack4.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack4.get(1),
								OpponentPlayer.stack4.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
				if (OpponentPlayer.stack4.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack4.get(2),
								OpponentPlayer.stack4.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
			}
		}
	}

	public void onClickstack4card2(View view) {
		Game.stack = 4;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack4.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack4.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack4.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack4.get(index),
						OpponentPlayer.stack4.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 4;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack4.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack4.get(0),
								OpponentPlayer.stack4.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 4;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack4.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack4.get(1),
								OpponentPlayer.stack4.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
				if (OpponentPlayer.stack4.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack4.get(2),
								OpponentPlayer.stack4.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
			}
		}
	}

	public void onClickstack4card3(View view) {
		Game.stack = 4;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack4.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack4.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack4.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack4.get(index),
						OpponentPlayer.stack4.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 4;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack4.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack4.get(0),
								OpponentPlayer.stack4.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 4;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack4.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack4.get(1),
								OpponentPlayer.stack4.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
				if (OpponentPlayer.stack4.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack4.get(2),
								OpponentPlayer.stack4.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
			}
		}
	}

	public void onClickstack5card1(View view) {
		Game.stack = 5;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack5.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack5.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack5.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack5.get(index),
						OpponentPlayer.stack5.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 5;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack5.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack5.get(0),
								OpponentPlayer.stack5.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 5;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack5.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack5.get(1),
								OpponentPlayer.stack5.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
				if (OpponentPlayer.stack5.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack5.get(2),
								OpponentPlayer.stack5.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
			}
		}
	}

	public void onClickstack5card2(View view) {
		Game.stack = 5;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack5.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack5.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack5.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack5.get(index),
						OpponentPlayer.stack5.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 5;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack5.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack5.get(0),
								OpponentPlayer.stack5.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 5;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack5.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack5.get(1),
								OpponentPlayer.stack5.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
				if (OpponentPlayer.stack5.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack5.get(2),
								OpponentPlayer.stack5.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
			}
		}
	}

	public void onClickstack5card3(View view) {
		Game.stack = 5;
		int count = 0;
		int index = 0;
		if (OpponentPlayer.stack5.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (OpponentPlayer.stack5.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (OpponentPlayer.stack5.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						OpponentPlayer.stack5.get(index),
						OpponentPlayer.stack5.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}

			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 5;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (OpponentPlayer.stack5.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack5.get(0),
								OpponentPlayer.stack5.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 5;
					Game.stackindex = 0;
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (OpponentPlayer.stack5.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);

					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack5.get(1),
								OpponentPlayer.stack5.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					button.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
				if (OpponentPlayer.stack5.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.opponentright_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack5.get(2),
								OpponentPlayer.stack5.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
			}
		}
	}

	public void onClickmystack1card1(View View) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack1.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack1.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack1.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack1.get(index),
						Player.stack1.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
			Game.stack = 1;
			Game.stackindex = index;
		} else if (count > 1) {
			while (true) {
				if (Player.stack1.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack1.get(0),
								Player.stack1.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 1;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack1.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack1.get(1),
								Player.stack1.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
				if (Player.stack1.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack1.get(2),
								Player.stack1.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
			}
		}
	}

	public void onClickmystack1card2(View View) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack1.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack1.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack1.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack1.get(index),
						Player.stack1.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 1;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack1.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack1.get(0),
								Player.stack1.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 1;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack1.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack1.get(1),
								Player.stack1.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
				if (Player.stack1.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack1.get(2),
								Player.stack1.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
			}
		}

	}

	public void onClickmystack1card3(View View) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack1.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack1.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack1.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack1.get(index),
						Player.stack1.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 1;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack1.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack1.get(0),
								Player.stack1.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 1;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack1.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack1.get(1),
								Player.stack1.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
				if (Player.stack1.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack1.get(2),
								Player.stack1.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
			}
		}

	}

	public void onClickmystack2card1(View View) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack2.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack2.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack2.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack2.get(index),
						Player.stack2.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 2;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack2.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack2.get(0),
								Player.stack2.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 2;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack2.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack2.get(1),
								Player.stack2.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
				if (Player.stack2.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack2.get(2),
								Player.stack2.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 1;
					break;
				}
			}
		}
	}

	public void onClickmystack2card2(View View) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack2.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack2.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack2.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack2.get(index),
						Player.stack2.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 2;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack2.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack2.get(0),
								Player.stack2.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 2;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack2.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack2.get(1),
								Player.stack2.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 2;
					break;
				}
				if (Player.stack2.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack2.get(2),
								Player.stack2.get(2).cardID));
					} catch (IOException e) {
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 2;
					break;
				}
			}
		}
	}

	public void onClickmystack2card3(View View) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack2.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack2.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack2.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack2.get(index),
						Player.stack2.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 2;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack2.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack2.get(0),
								Player.stack2.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 2;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack2.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack2.get(1),
								Player.stack2.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 2;
					break;
				}
				if (Player.stack2.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack2.get(2),
								Player.stack2.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 2;
					break;
				}
			}
		}
	}

	public void onClickmystack3card1(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack3.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack3.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack3.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack3.get(index),
						Player.stack3.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 3;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack3.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack3.get(0),
								Player.stack3.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 3;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack3.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack3.get(1),
								Player.stack3.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
				if (Player.stack3.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack3.get(2),
								Player.stack3.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
			}
		}
	}

	public void onClickmystack3card2(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack3.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack3.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack3.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack3.get(index),
						Player.stack3.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 3;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack3.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack3.get(0),
								Player.stack3.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 3;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack3.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack3.get(1),
								Player.stack3.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
				if (Player.stack3.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack3.get(2),
								Player.stack3.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
			}
		}
	}

	public void onClickmystack3card3(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack3.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack3.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack3.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack3.get(index),
						Player.stack3.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 3;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack3.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack3.get(0),
								Player.stack3.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 3;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack3.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack3.get(1),
								Player.stack3.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
				if (Player.stack3.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack3.get(2),
								Player.stack3.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 3;
					break;
				}
			}
		}
	}

	public void onClickmystack4card1(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack4.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack4.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack4.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack4.get(index),
						Player.stack4.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 4;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack4.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack4.get(0),
								Player.stack4.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 4;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack4.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack4.get(1),
								Player.stack4.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
				if (Player.stack4.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack4.get(2),
								Player.stack4.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
			}
		}
	}

	public void onClickmystack4card2(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack4.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack4.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack4.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack4.get(index),
						Player.stack4.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 4;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack4.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack4.get(0),
								Player.stack4.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 4;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack4.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack4.get(1),
								Player.stack4.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
				if (Player.stack4.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack4.get(2),
								Player.stack4.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
			}
		}
	}

	public void onClickmystack4card3(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack4.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack4.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack4.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack4.get(index),
						Player.stack4.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 4;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack4.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack4.get(0),
								Player.stack4.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 4;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack4.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack4.get(1),
								Player.stack4.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
				if (Player.stack4.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack4.get(2),
								Player.stack4.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 4;
					break;
				}
			}
		}
	}

	public void onClickmystack5card1(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack5.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack5.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack5.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack5.get(index),
						Player.stack5.get(index).cardID));
			} catch (IOException e) {
				e.printStackTrace();
			}
			Game.stack = 5;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack5.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack5.get(0),
								Player.stack5.get(0).cardID));
					} catch (IOException e) {
						e.printStackTrace();
					}
					Game.stack = 5;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack5.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack5.get(1),
								Player.stack5.get(1).cardID));
					} catch (IOException e) {
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
				if (Player.stack5.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack5.get(2),
								Player.stack5.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
			}
		}
	}

	public void onClickmystack5card2(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack5.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack5.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack5.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack5.get(index),
						Player.stack5.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 5;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack5.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack5.get(0),
								Player.stack5.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 5;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack5.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack5.get(1),
								Player.stack5.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
				if (Player.stack5.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack5.get(2),
								Player.stack5.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
			}
		}
	}

	public void onClickmystack5card3(View view) {
		Game.stack = 1;
		int count = 0;
		int index = 0;
		if (Player.stack5.get(0).cardID != 0) {
			index = 0;
			count++;
		}
		if (Player.stack5.get(1).cardID != 0) {
			index = 1;
			count++;

		}
		if (Player.stack5.get(2).cardID != 0) {
			index = 2;
			count++;

		}
		if (count == 1) {
			int id = getResources().getIdentifier("sample_0", "drawable",
					getPackageName());
			ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
			Button cancel = (Button) findViewById(R.id.cancel);
			Button discard = (Button) findViewById(R.id.discard);
			try {
				recyclebitmap(zoomcard);
				zoomcard.setImageDrawable(writeOnDrawable(id,
						Player.stack5.get(index),
						Player.stack5.get(index).cardID));
			} catch (IOException e) {
				//
				e.printStackTrace();
			}
			Game.stack = 5;
			Game.stackindex = index;
			discard.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.VISIBLE);
			zoomcard.setVisibility(View.VISIBLE);
		} else if (count > 1) {
			while (true) {
				if (Player.stack5.get(0).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack5.get(0),
								Player.stack5.get(0).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stack = 5;
					Game.stackindex = 0;
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					zoomcard.setVisibility(View.VISIBLE);
					break;
				}
				if (Player.stack5.get(1).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack5.get(1),
								Player.stack5.get(1).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					discard.setVisibility(View.VISIBLE);
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 1;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
				if (Player.stack5.get(2).cardID != 0) {
					int id = getResources().getIdentifier("sample_0",
							"drawable", getPackageName());
					ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
					ImageButton button = (ImageButton) findViewById(R.id.right_button);
					Button cancel = (Button) findViewById(R.id.cancel);
					Button discard = (Button) findViewById(R.id.discard);
					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack5.get(2),
								Player.stack5.get(2).cardID));
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					cancel.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					button.setVisibility(View.VISIBLE);
					Game.stackindex = 2;
					zoomcard.setVisibility(View.VISIBLE);
					Game.stack = 5;
					break;
				}
			}
		}
	}

	public int getopponentnumcards(int i) {
		int count = 0;
		switch (i) {

		case 1:
			if (OpponentPlayer.stack1.get(0).cardID != 0) {
				count++;
			}
			if (OpponentPlayer.stack1.get(1).cardID != 0) {
				count++;

			}
			if (OpponentPlayer.stack1.get(2).cardID != 0) {
				count++;

			}
			break;
		case 2:
			if (OpponentPlayer.stack2.get(0).cardID != 0) {
				count++;
			}
			if (OpponentPlayer.stack2.get(1).cardID != 0) {
				count++;

			}
			if (OpponentPlayer.stack2.get(2).cardID != 0) {
				count++;

			}
			break;

		case 3:
			if (OpponentPlayer.stack3.get(0).cardID != 0) {
				count++;
			}
			if (OpponentPlayer.stack3.get(1).cardID != 0) {
				count++;

			}
			if (OpponentPlayer.stack3.get(2).cardID != 0) {
				count++;

			}
			break;
		case 4:
			if (OpponentPlayer.stack4.get(0).cardID != 0) {
				count++;
			}
			if (OpponentPlayer.stack4.get(1).cardID != 0) {
				count++;

			}
			if (OpponentPlayer.stack4.get(2).cardID != 0) {
				count++;

			}
			break;
		case 5:
			if (OpponentPlayer.stack5.get(0).cardID != 0) {
				count++;
			}
			if (OpponentPlayer.stack5.get(1).cardID != 0) {
				count++;

			}
			if (OpponentPlayer.stack5.get(2).cardID != 0) {
				count++;

			}
			break;

		}
		return count;
	}

	public int getnumcards(int i) {

		int count = 0;
		switch (i) {

		case 1:
			if (Player.stack1.get(0).cardID != 0) {
				count++;
			}
			if (Player.stack1.get(1).cardID != 0) {
				count++;

			}
			if (Player.stack1.get(2).cardID != 0) {
				count++;

			}
			break;
		case 2:
			if (Player.stack2.get(0).cardID != 0) {
				count++;
			}
			if (Player.stack2.get(1).cardID != 0) {
				count++;

			}
			if (Player.stack2.get(2).cardID != 0) {
				count++;

			}
			break;

		case 3:
			if (Player.stack3.get(0).cardID != 0) {
				count++;
			}
			if (Player.stack3.get(1).cardID != 0) {
				count++;

			}
			if (Player.stack3.get(2).cardID != 0) {
				count++;

			}
			break;
		case 4:
			if (Player.stack4.get(0).cardID != 0) {
				count++;
			}
			if (Player.stack4.get(1).cardID != 0) {
				count++;

			}
			if (Player.stack4.get(2).cardID != 0) {
				count++;

			}
			break;
		case 5:
			if (Player.stack5.get(0).cardID != 0) {
				count++;
			}
			if (Player.stack5.get(1).cardID != 0) {
				count++;

			}
			if (Player.stack5.get(2).cardID != 0) {
				count++;

			}
			break;

		}
		return count;
	}

	public void onClickOpponentRightButton(View view) {
		int id = getResources().getIdentifier("sample_0", "drawable",
				getPackageName());
		int imageid = 0;
		ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
		;

		switch (Game.stack) {

		case 1:
			for (int i = (Game.stackindex + 1) % 3; i < 3; i = (i + 1) % 3) {
				if (OpponentPlayer.stack1.get(i).cardID != 0) {
					if (i == 0)
						imageid = R.id.stack1card1;
					if (i == 1)
						imageid = R.id.stack1card2;
					if (i == 2)
						imageid = R.id.stack1card3;

					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack1.get(i),
								OpponentPlayer.stack1.get(i).cardID));
						zoomcard.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stackindex = i;
					break;
				}
			}
			break;

		case 2:
			for (int i = (Game.stackindex + 1) % 3; i < 3; i = (i + 1) % 3) {
				if (OpponentPlayer.stack2.get(i).cardID != 0) {
					if (i == 0)
						imageid = R.id.stack2card1;
					if (i == 1)
						imageid = R.id.stack2card2;
					if (i == 2)
						imageid = R.id.stack2card3;

					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack2.get(i),
								OpponentPlayer.stack2.get(i).cardID));
						zoomcard.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stackindex = i;
					break;
				}
			}
			break;
		case 3:
			for (int i = (Game.stackindex + 1) % 3; i < 3; i = (i + 1) % 3) {
				if (OpponentPlayer.stack3.get(i).cardID != 0) {
					if (i == 0)
						imageid = R.id.stack3card1;
					if (i == 1)
						imageid = R.id.stack3card2;
					if (i == 2)
						imageid = R.id.stack3card3;

					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack3.get(i),
								OpponentPlayer.stack3.get(i).cardID));
						zoomcard.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stackindex = i;
					break;
				}
			}
			break;

		case 4:
			for (int i = (Game.stackindex + 1) % 3; i < 3; i = (i + 1) % 3) {
				if (OpponentPlayer.stack4.get(i).cardID != 0) {
					if (i == 0)
						imageid = R.id.stack4card1;
					if (i == 1)
						imageid = R.id.stack4card2;
					if (i == 2)
						imageid = R.id.stack4card3;

					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack4.get(i),
								OpponentPlayer.stack4.get(i).cardID));
						zoomcard.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stackindex = i;
					break;
				}
			}
			break;
		case 5:
			for (int i = (Game.stackindex + 1) % 3; i < 3; i = (i + 1) % 3) {
				if (OpponentPlayer.stack5.get(i).cardID != 0) {
					if (i == 0)
						imageid = R.id.stack5card1;
					if (i == 1)
						imageid = R.id.stack5card2;
					if (i == 2)
						imageid = R.id.stack5card3;

					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								OpponentPlayer.stack5.get(i),
								OpponentPlayer.stack5.get(i).cardID));
						zoomcard.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stackindex = i;
					break;
				}
			}
			break;
		}

		if (getopponentnumcards(Game.stack) == 1) {
			ImageButton button = (ImageButton) findViewById(R.id.right_button);
			button.setVisibility(View.INVISIBLE);
		}
	}

	public void onClickRightButton(View view) {

		int id = getResources().getIdentifier("sample_0", "drawable",
				getPackageName());
		int imageid = 0;
		ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
		;

		switch (Game.stack) {

		case 1:
			for (int i = (Game.stackindex + 1) % 3; i < 3; i = (i + 1) % 3) {
				if (Player.stack1.get(i).cardID != 0) {
					if (i == 0)
						imageid = R.id.mystack1card1;
					if (i == 1)
						imageid = R.id.mystack1card2;
					if (i == 2)
						imageid = R.id.mystack1card3;

					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack1.get(i),
								Player.stack1.get(i).cardID));
						zoomcard.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stackindex = i;
					break;
				}
			}
			break;

		case 2:
			for (int i = (Game.stackindex + 1) % 3; i < 3; i = (i + 1) % 3) {
				if (Player.stack2.get(i).cardID != 0) {
					if (i == 0)
						imageid = R.id.mystack2card1;
					if (i == 1)
						imageid = R.id.mystack2card2;
					if (i == 2)
						imageid = R.id.mystack2card3;

					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack2.get(i),
								Player.stack2.get(i).cardID));
						zoomcard.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stackindex = i;
					break;
				}
			}
			break;
		case 3:
			for (int i = (Game.stackindex + 1) % 3; i < 3; i = (i + 1) % 3) {
				if (Player.stack3.get(i).cardID != 0) {
					if (i == 0)
						imageid = R.id.mystack3card1;
					if (i == 1)
						imageid = R.id.mystack3card2;
					if (i == 2)
						imageid = R.id.mystack3card3;

					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack3.get(i),
								Player.stack3.get(i).cardID));
						zoomcard.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						//
						e.printStackTrace();
					}
					Game.stackindex = i;
					break;
				}
			}
			break;

		case 4:
			for (int i = (Game.stackindex + 1) % 3; i < 3; i = (i + 1) % 3) {
				if (Player.stack4.get(i).cardID != 0) {
					if (i == 0)
						imageid = R.id.mystack4card1;
					if (i == 1)
						imageid = R.id.mystack4card2;
					if (i == 2)
						imageid = R.id.mystack4card3;

					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack4.get(i),
								Player.stack4.get(i).cardID));
						zoomcard.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						e.printStackTrace();
					}
					Game.stackindex = i;
					break;
				}
			}
			break;
		case 5:
			for (int i = (Game.stackindex + 1) % 3; i < 3; i = (i + 1) % 3) {
				if (Player.stack5.get(i).cardID != 0) {
					if (i == 0)
						imageid = R.id.mystack5card1;
					if (i == 1)
						imageid = R.id.mystack5card2;
					if (i == 2)
						imageid = R.id.mystack5card3;

					try {
						recyclebitmap(zoomcard);
						zoomcard.setImageDrawable(writeOnDrawable(id,
								Player.stack5.get(i),
								Player.stack5.get(i).cardID));
						zoomcard.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						e.printStackTrace();
					}
					Game.stackindex = i;
					break;
				}
			}
			break;
		}

		if (getnumcards(Game.stack) == 1) {
			ImageButton button = (ImageButton) findViewById(R.id.right_button);
			button.setVisibility(View.INVISIBLE);
		}
	}

	public void onClickCancel(View view) {
		ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
		ImageButton button = (ImageButton) findViewById(R.id.right_button);
		ImageButton Opponentbutton = (ImageButton) findViewById(R.id.opponentright_button);
		Button cancel = (Button) findViewById(R.id.cancel);
		Button discard = (Button) findViewById(R.id.discard);

		cancel.setVisibility(View.INVISIBLE);
		Opponentbutton.setVisibility(View.INVISIBLE);
		button.setVisibility(View.INVISIBLE);
		zoomcard.setVisibility(View.INVISIBLE);
		discard.setVisibility(View.INVISIBLE);

	}

	public void onClickDiscard(View view) {

		switch (Game.stack) {
		case 1:
			if (Game.stackindex == 0) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack1card1);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack1.set(0, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();

										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}
									}
								}).create().show();

			}
			if (Game.stackindex == 1) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack1card2);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack1.set(1, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}

									}
								}).create().show();
			}
			if (Game.stackindex == 2) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack1card3);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack1.set(2, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}

									}
								}).create().show();
			}
			break;
		case 2:
			if (Game.stackindex == 0) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack2card1);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack2.set(0, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}
									}
								}).create().show();

			}
			if (Game.stackindex == 1) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack2card2);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack2.set(1, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}

									}
								}).create().show();
			}
			if (Game.stackindex == 2) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack2card3);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack2.set(2, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}

									}
								}).create().show();
			}
			break;
		case 3:
			if (Game.stackindex == 0) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack3card1);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack3.set(0, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}
									}
								}).create().show();

			}
			if (Game.stackindex == 1) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack3card2);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack3.set(1, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}

									}
								}).create().show();
			}
			if (Game.stackindex == 2) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack3card3);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack3.set(2, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}

									}
								}).create().show();
			}
			break;
		case 4:
			if (Game.stackindex == 0) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack4card1);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack4.set(0, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}
									}
								}).create().show();

			}
			if (Game.stackindex == 1) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack4card2);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack4.set(1, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}

									}
								}).create().show();
			}
			if (Game.stackindex == 2) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack4card3);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack4.set(2, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}

									}
								}).create().show();
			}
			break;
		case 5:
			if (Game.stackindex == 0) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack5card1);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack5.set(0, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}
									}
								}).create().show();

			}
			if (Game.stackindex == 1) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack5card2);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack5.set(1, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}

									}
								}).create().show();
			}
			if (Game.stackindex == 2) {
				new AlertDialog.Builder(this)
						.setTitle("Really Discard?")
						.setMessage(
								"Are you sure you want to discard this card")
						.setNegativeButton(android.R.string.no, null)
						.setPositiveButton(android.R.string.yes,
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										if (Player.candiscard) {
											ImageView image = (ImageView) findViewById(R.id.mystack5card3);
											image.clearAnimation();
											image.setVisibility(View.INVISIBLE);
											ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image4);
											zoomcard.setVisibility(View.INVISIBLE);
											CurrentCard card = new CurrentCard(
													0);
											Player.stack5.set(2, card);
											Player.candiscard = false;
											updatelevels();
											updatestrength();
										} else {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													GamePlay.this);
											builder.setMessage(
													"You have already discarded 1 card")
													.setNegativeButton(
															"Ok",
															new DialogInterface.OnClickListener() {
																@Override
																public void onClick(
																		DialogInterface dialog,
																		int id) {

																	// User
																	// cancelled
																	// the
																	// dialog
																}
															});
											// Create the AlertDialog object and
											// return it
											builder.create();
											builder.show();
										}

									}
								}).create().show();
			}
			break;
		}
	}
	private Runnable updateboardloop = new Runnable() {

		public void run() {

			// try {
			new UpdateBoard().execute(getApplicationContext());
			// updateboard();
			// } catch (JSONException e) {
			//
			// e.printStackTrace();
			// }

		}

	};

	public void onClickImage1(View view) {
		ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image1);
		zoomcard.setVisibility(View.VISIBLE);

	}

	public void onClickImage2(View view) {
		ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image2);
		zoomcard.setVisibility(View.VISIBLE);

	}

	public void onClickImage3(View view) {
		ImageView zoomcard = (ImageView) findViewById(R.id.expanded_image3);
		zoomcard.setVisibility(View.VISIBLE);

	}

	@Override
	public void onBackPressed() {
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (task2 != null && !task2.isCancelled())
			task2.cancel(true);
		Player.mydeck = new ArrayList<CurrentCard>();
		CardLibrary.cardlibrary = new ArrayList<CurrentCard>();
		Bitmaparray.bitmapArray = new ArrayList<Bitmap>();
		Player.myhand = new ArrayList<CurrentCard>(6);
		Player.stack1 = new ArrayList<CurrentCard>();
		Player.stack2 = new ArrayList<CurrentCard>();
		Player.stack3 = new ArrayList<CurrentCard>();
		Player.stack4 = new ArrayList<CurrentCard>();
		Player.stack5 = new ArrayList<CurrentCard>();
		OpponentPlayer.stack1 = new ArrayList<CurrentCard>();
		OpponentPlayer.stack2 = new ArrayList<CurrentCard>();
		OpponentPlayer.stack3 = new ArrayList<CurrentCard>();
		OpponentPlayer.stack4 = new ArrayList<CurrentCard>();
		OpponentPlayer.stack5 = new ArrayList<CurrentCard>();
		Counter.count = 0;
		OpponentPlayer.power = BigRational.valueOf("0");
		OpponentPlayer.health = BigRational.valueOf("20");
		Player.canplayresource = true;
		Player.finished = false;
		Player.waschallenged = false;
		Player.endturn = true;
		Player.candiscard = true;
		Player.challenger = false;
		Player.challenged = false;
		Player.health = BigRational.valueOf("20");
		Player.donewaiting = true;
		Game.firstturn = true;
		Game.mydiceroll = 0;
		Game.opponentdiceroll = 0;
		Game.exit = true;
		recyclebitmaps();
		unbindDrawables(findViewById(R.id.RelativeLayout));
		System.gc();
		//logHeap();

	}
	private void recyclebitmap(ImageView img) {
		ImageView myimg = img;
		Drawable drawable;
		drawable = img.getDrawable();
		if (drawable instanceof BitmapDrawable) {
			BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
			if (null != bitmapDrawable
					&& !bitmapDrawable.getBitmap().isRecycled()) {
				bitmapDrawable.getBitmap().recycle();
			} else {

				Log.e("recycled", "bitmap is already recycled");
			}
			bitmapDrawable = null;

		}
	}
	private void recyclebitmap(int id) {
		ImageView img = (ImageView) findViewById(id);
		Drawable drawable;

		drawable = img.getDrawable();
		if (drawable instanceof BitmapDrawable) {
			BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
			if (null != bitmapDrawable
					&& !bitmapDrawable.getBitmap().isRecycled()) {
				bitmapDrawable.getBitmap().recycle();
			} else {

				Log.e("recycled", "bitmap is already recycled");
			}
			bitmapDrawable = null;

		}
	}
	private void recyclebitmaps() {
		recyclebitmap(R.id.myhandcard1);
		recyclebitmap(R.id.myhandcard2);
		recyclebitmap(R.id.myhandcard3);
		recyclebitmap(R.id.myhandcard4);
		recyclebitmap(R.id.myhandcard5);
		recyclebitmap(R.id.myhandcard6);

		recyclebitmap(R.id.mystack1card1);
		recyclebitmap(R.id.mystack1card2);
		recyclebitmap(R.id.mystack1card3);

		recyclebitmap(R.id.mystack2card1);
		recyclebitmap(R.id.mystack2card2);
		recyclebitmap(R.id.mystack2card3);

		recyclebitmap(R.id.mystack3card1);
		recyclebitmap(R.id.mystack3card2);
		recyclebitmap(R.id.mystack3card3);

		recyclebitmap(R.id.mystack4card1);
		recyclebitmap(R.id.mystack4card2);
		recyclebitmap(R.id.mystack4card3);

		recyclebitmap(R.id.mystack5card1);
		recyclebitmap(R.id.mystack5card2);
		recyclebitmap(R.id.mystack5card3);

		recyclebitmap(R.id.stack1card1);
		recyclebitmap(R.id.stack1card2);
		recyclebitmap(R.id.stack1card3);

		recyclebitmap(R.id.stack2card1);
		recyclebitmap(R.id.stack2card2);
		recyclebitmap(R.id.stack2card3);

		recyclebitmap(R.id.stack3card1);
		recyclebitmap(R.id.stack3card2);
		recyclebitmap(R.id.stack3card3);

		recyclebitmap(R.id.stack4card1);
		recyclebitmap(R.id.stack4card2);
		recyclebitmap(R.id.stack4card3);

		recyclebitmap(R.id.stack5card1);
		recyclebitmap(R.id.stack5card2);
		recyclebitmap(R.id.stack5card3);

		recyclebitmap(R.id.expanded_image);
		recyclebitmap(R.id.expanded_image4);

	}
	private void unbindDrawables(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
		}
		if (view instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				unbindDrawables(((ViewGroup) view).getChildAt(i));
			}
			((ViewGroup) view).removeAllViews();
		}
	}

	@Override
	protected void onPostResume() {
		if (Globals.focusedActivity.equals("GamePlay")) {
			if (Globals.settingsHolder[1] == 1) {
				startService(Globals.backgroundMusicService);
			}
			Globals.isMusicPlaying = true;
		}
		super.onPostResume();
	}

	@Override
	protected void onPause() {
		if (this.isFinishing()) {
			Globals.focusedActivity = "GameMenu";
		} else {
			if (Globals.settingsHolder[1] == 1) {
				stopService(Globals.backgroundMusicService);
			}
			Globals.isMusicPlaying = false;
		}
		super.onPause();
	}
}
